-2

So i have to take the values from an array i made in a function and use that array in another function. How do i do that? Pretty much the values i need are those of 'div_array' 'rows' and 'cols'. I used "return div_array, rows, cols;" but i dont think thats how it works. How can i take their value after the calculations and use them in another function?

int DivisionArray(int ** div_array, int *array, int N, int rows, int cols) {
int counter = 1;
for (int i = 1; i <= N - 1; i++) {
    for (int j = 1; j <= N - 1; j++) {
        for (int k = 1; k <= N - 1; k++) {
            if ((array[i] * array[j]) - (N*k) == 1) {
                counter++;
            }
        }
    }
}
cols = counter;
div_array[0][0] = 1;
div_array[1][0] = 1;
int temp = 1;
for (int i = 1; i <= N - 1; i++) {
    for (int j = 1; j <= N - 1; j++) {
        for (int k = 1; k <= N - 1; k++) {
            if ((array[i] * array[j]) - (N*k) == 1) {
                div_array[0][temp] = array[i];
                div_array[1][temp] = array[j];
                temp++;
            }
        }
    }
}
for (int i = 0; i <= rows - 1; i++) {
    for (int j = 0; j <= cols - 1; j++) {
        cout << div_array[i][j] << "\t";
    }
    cout << endl;
}
return div_array, rows, cols;
}
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • in c++ use references for your function paramaters. – πάντα ῥεῖ Dec 23 '18 at 14:38
  • This `return div_array, rows, cols;` doesn't work. Read [How to return multiple value from a function](https://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function) – Achal Dec 23 '18 at 14:39
  • 1
    c++ doesn't allow you to return multiple value. Pack them in a struct/tuple and return that. However, it seems like all you need here is to start using the proper tools for your code. `std::vector` will solve your issue since the size of the container is automatically passed along with it. – super Dec 23 '18 at 14:40
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. Concerning the question you asked, also consider extracting a [mcve]. Please also read the description of tags that you apply, in this case in particular the "c" tag. – Ulrich Eckhardt Dec 23 '18 at 14:41

1 Answers1

-1

As mentioned in the comment section, you can't return multiple values from a function with the return statement in C++. However, there are other ways to do that. You can use a struct;

auto DivisionArray(int ** div_array, int *array, int N, int rows, int cols) {

/// previous codes before return


    struct divArray {
        int **array; 
        int row;
        int col;
    };

    return divArray {div_array, rows, cols};

}

After C++11, you can use std::pair or std::tuple as well for the same purpose with a struct.

Ozan Yurtsever
  • 1,274
  • 8
  • 32