I am stuck on the last few problems of an array exercise. Could anyone lend a hand?
Write C++ statements that do the following:
-
store 5 in the first column of an array and make sure that the value in each subsequent column is twice the value in the previous column.
-
print the array one row per line.
-
print the array one column per line.
I think this will work for question #2:
for (row = 0; row < 10; row++)
{
for (col = 0; col < 20; col++)
cout << alpha[row][col] << " ";
cout << endl;
}
but question 1 and 3 have me stumped. thanks
Here’s what i came up with after your tips. thanks everyone
3.
for (col = 0; col < 20; ++col)
{
for (row = 0; row < 20; ++row)
cout << alpha[row][col] << " ";
cout << endl;
}
1.
for (row = 0; row < 10; row++)
alpha[row][0] = 5;
for (col = 1; col < 20; col++)
for (row = 0; row < 10; row++)
alpha[row][col]=alpha[row][col-1]*2;
For #1, run a loop that starts at zero and goes until the number of rows. In each iteration just assign 5 to
array[row][0]=5(since coloumn 0 is the first coloumn).Now run a loop from 1 to the number of coloumns. Inside, run another loop for each row. just assign
array[row][col]=array[row][col-1]*2.For #3, simply reverse the order of the loops. We iterate over all coloumns, and for each coloumns we have to iterate over all rows and print a newline after that.
I would post code, but it is better for you to try to understand and write the code yourself.