Within your for loop you would want to use i and j as the indices
array[i][j]
. row and col are constant on every iteration of the for loop statements. They are constant because no new values are assigned to them. However, i and j are increased by one every time.
++i
is basically equivalent to
i = 1 + i
. Of course you will want to change the indices on line 41 and line 43.
Also at some point you are going to need command line arguments so you should have
int main(int argc, char * argv[])
instead of
int main()
.
I made the changes to your code that I suggested and had this as output:
WELCOME!
Columns
> 5
Rows
> 8
58 22 58 10 96
25 98 62 28 5
93 21 67 72 5
16 45 80 15 1
96 76 24 24 51
90 88 27 65 30
68 78 90 95 25
74 18 83 26 30 |
Your next step is going to be passing this array back to main so main can use it in another function. This other function is what finds the largest product, but it is going to need the array as input. I think if you can understand passing some normal variables by reference, it will help you in passing an array by reference.
http://www.cplusplus.com/doc/tutorial/functions2/
So now your goal should be making 2 functions and main. One function creates the array, then main takes the created array and passes it the other function, and for now just make the other function print out the array to see if it worked correctly. Then you can worry about making the other function find the largest product.
Edit:
I changed the code by swapping row and col, and leaving i and j in order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void create_array()
{
int col, row;
cout << "Columns\n> " << flush;
cin >> col;
cout << "Rows\n> "<< flush;
cin >> row;
int array[row][col];
srand(time(NULL));
for( int i = 0; i <row; ++i, cout << endl )
for( int j = 0; j <col; ++j )
cout << setw(3) << (array[i][j] = (rand() % 100) + 1) << " ";
}
|
The setw modifier requires including this at the top:
#include <iomanip>