Correcting test questions

closed account (i8bjz8AR)
A c++ program uses a two dimensional array defined as follows:

Const Int NUM_ROWS =40
const Int NUM_ROWS = 10;
int grades[NUM_ROWS][NUM_COLS];

*Write code that sums each row in the array and then displays the results.
*Write code that sums each column in the array and then displays the results.

This is going to be on my final and I got pretty lost on it when it was on our test. I did something like this for both:
1
2
3
4
5
6
7
8
9
  #include <iostream>
Using namespace std;
Int main () 
Int sum =0;
For (i=0;i <NUM_ROWS;I++) 
{
grades [NUM_ROWS] += sum;
} 
One way to answer these questions is to write code for a complete example. Compile and run it. Then verify that the output matches what you get when you do the calculation by hand. You'll learn more, and remember better, by actually doing.
Think about how the index for your row might change as you're totaling up a column, and how the column index might change.

Likewise for summing up a row. How would each index change?

Hint: One of them will be constant for each situation.
closed account (i8bjz8AR)
Okay would you be able to get me started or direct me to a good example of this?
First set up the array. See the example:
Initializing Two-Dimensional Arrays
on this page
http://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm
But use the constants NUM_ROWS and NUM_COLS instead of the 3, 4 in the example.

Once you've done that you can write and test the code as required by the original question.


I'd suggest for ease of testing, there's no need to use the original 40x10, start with something smaller that is easier to both write the code for and to check,
Last edited on
Topic archived. No new replies allowed.