简体   繁体   中英

How to pass array 2d as parameter from function and return array 2d this function?

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;
}

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 . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM