0

The problem in question involves an iterative process in which the object "am" changes with this process (loop). So, I need to create a "res" object that will store the results of the Tfunc function to be used in the same process in another procedure. However, I can not do that. I'm a good beginner in C ++ programming. I'm a programmer in the R language.

My operational system is ubuntu 16.04 using ide codeblocks.

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

const int k = 3;

double Tfunc(double Told[k][k], double amm, int K);

int sample(int x);

int main(){
 int i, j;
 double am = 50;
 double told[k][k]{
  {1,0,0},
  {0,1,0},
  {0,0,1}
};

  double res;

  res = Tfunc(told, am, k);

  for(i=0;i<k;i++){
      for(j=0;j<k;j++){
          cout << res[i][j] << " ";
      }
      cout << "\n";
  }

  return 0;
}

double Tfunc(double Told[k][k], double amm, int K)
{
 int id1;
 int id2;
 int id3;

 id1 = sample(K);
 id2 = sample(K);
 id3 = sample(K);

 while(id2 == id3){
  id3 = sample(K);
 }

 Told[id1][id2] = Told[id1][id2] + amm;
 Told[id1][id3] = Told[id1][id3] - amm;

 return Told;
}

int sample(int x)
{
 srand(time(NULL)); //initialize the random seed

 int RandIndex = rand() % (x); 
 return RandIndex;
}
  • Use `std::vector>` instead. Find a tutorial on how use standard library containers such as `vector`. – Nikos C. Jun 15 '19 at 13:27
  • 3
    It's probably easier to use a `std::array>` to do this. `std::vector>` is another (more flexible) alternative. – πάντα ῥεῖ Jun 15 '19 at 13:27
  • @πάνταῥεῖ Agree, `std::array` is much more efficient here and applicable because fixed size. Just a note. s/b `std::array,k>` – doug Jun 15 '19 at 15:38

1 Answers1

0

First of all, and, maybe this is one of the root causes of your problem, you are not passing the 2 dimensional array to your subfunction correctly.

You could pass it as reference or as pointer. Then you could also modify the array, given as parameter, in your subfunction.

Please read here

Then, in modern C++ you would use STL containers for your puposes. And for 2 dimensional stuff, you need to create a container of containers. So a std::vector of std::vector, or a std::array of std::array.

The Tfunc will return that container-container and make usage of RVO (Return Value Optimization). So there is no loss in complexity.

I created an example file for you. This is just one possible solution. There are many.

#include <iostream>
#include <random>
#include <iterator>
#include <algorithm>
#include <array>

constexpr size_t MatrixDimension = 3;
using DoubleArray = std::array<double, MatrixDimension>;
using Matrix = std::array<DoubleArray, MatrixDimension>;

constexpr Matrix StartMatrix{{
 {1.0, 0.0, 0.0},
 {0.0, 1.0, 0.0},
 {0.0, 0.0, 1.0}
}};

size_t randomIndex()
{
    std::random_device randomDevice;    // Obtain a random number from hardware
    std::mt19937 randomGenerator(randomDevice());  // Seed the generator
    std::uniform_int_distribution<size_t> distribution(0, MatrixDimension-1); // Range
    return distribution(randomGenerator);
}

Matrix Tfunc(const Matrix& givenMatrix, double amm)
{
    size_t index1{ randomIndex() }; // Set indices with random values
    size_t index2{ randomIndex() };
    size_t index3{ randomIndex() };

    while (index2 == index3) {          // Make sure that index 2 is not equal index 3
        index3 = randomIndex();
    }
    Matrix calculatedMatrix{};

    calculatedMatrix[index1][index2] = givenMatrix[index1][index2] + amm;
    calculatedMatrix[index1][index3] = givenMatrix[index1][index3] - amm;

    return calculatedMatrix;
}

int main()
{
    constexpr double amm{ 50.0 };
    Matrix result = Tfunc(StartMatrix, amm);  // Apply Tfunc to matrix

    // Debug Output.  Print matrix to std::cout
    std::for_each(result.begin(), result.end(), [](DoubleArray &da) {std::copy(da.begin(), da.end(), std::ostream_iterator<double>(std::cout, " ")); std::cout << '\n'; });

    return 0;
}

BTW. I do not know the purpose of your program. But I think you want to have 3 different indices in TFunc. This is not guaranteed. 2 can be the same.

I hope this helps . . .

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • Hi Armin. Thanks a lot for the help. I tried to run your code and realized that it went into an infinite loop. Placing in the main function only "std :: cout << randomIndex ();", I noticed that the same number is always generated. I tried to get around but I could not because of my experience with the language. If I still can help, I'll be very grateful. – Ivan Bezerra Allaman Jun 17 '19 at 15:20
  • I have tried to reproduce your problem, but I could not. I created a main with a loop and printed 100 times "randomIndex()" and got a different number any time. I am afraid that without seeing you code, I cannot help. – Armin Montigny Jun 20 '19 at 07:01