A function in my program should return two arrays and a number. The problem is that the two arrays are dynamic - I cannot pre-define their size. I've searched online and this example would work for fixed-length arrays:
void foo(int &fe, int &fi, int fo)
{
fe += fo;
fi += fe;
return;
}
...main()...
int A=0, B=1, C=2;
foo(A,B,C);
cout << A <<'\t'; //A=2
cout << B <<'\t'; //B=3
cout << C <<'\n'; //C=2
Which kind of makes sense to me: the "foo" function uses the address of whatever you pass on to it as the first and second arguments. It doesn't actually return anything but modifies the variables from within itself. I'd like to stick to this method because I can at least approximately understand what's going on. Could you please help me modify it (if possible) to a situatiohn when we'd like to pass a 1-D array and a 2-D grid created like this:
double *row ;
double **grid ;
cout << "Input the number of rows: " ;
cin >> ROWS ;
cout << "Input the number of columns: " ;
cin >> COLS ;
row = new double [COLS] ;
grid = new double *[ROWS] ;
for(i = 0 ; i < ROWS ; ++i) grid[i] = new double [COLS] ;
#include <iostream>
#include <new>
usingnamespace std ;
void test(int*& data, int n) ;
int main()
{
int n, i ;
int * data ;
cout << "How many numbers would you like? " ;
cin >> n ;
data = newint [n] ;
test(data, n) ;
for (i = 0; i < n; i++) cout << data[i] << "\n" ;
}
void test(int*& data, int n)
{
int i ;
for (i = 0; i <= (n - 1); i++) data[i] = i * 2 ;
}