need help with some arrays

1. Get students ID (1-10) and their test score (0-100) for 10 students using two dimensional array. This should be in a function call GetInput.
2. Create a function that calculate the average of class and print.
3. The last item in the array should store sum and average.
int student_record[11][2], student_record[10][0] should have sum and student_record[10][1] should have average.
4. Make sure you print out all student's ID and score in nice formatted text. And, print the average at the bottom.

ignoring the function get input and stuff, i want to know how i will get the sum of the student score in

student_record[10][0]

i also want to know how i will get the average in

student_record[10][1]

thanks a lot.
 ``12`` ``````student_record[10][0] = sum; student_record[10][1] = average;``````
sorry my wording was a bit wrong. i meant to say how would i add the sums up and find the average then place it in student_record[10][0] and student_record[10][1].

for example:

11 is too much so lets say student_record[4][2]

and this is my array chart
0 1
0 0 20
1 1 30
2 2 40
3 SUM AVERAGE

basically i want to add up the entire 1 column ( [0][1] , [1][1] , [2][1] ) and place it in sum. i then want to divide them by the number i added and place it inaverage.

Last edited on
To iterate through an array you can use a for loop.
Create a variable to store the sum
Loop through the rows of column 1 and add the number to the total each time

EDIT: now that I think about it the average will be stored as an int. If your total isn't exactly a multiple of the number of students the average score will be wrong
Last edited on
i know how to loop but idk how to loop while adding doing two dimensional.

can you demonstrate an example ?

thanks
I wanted to avoid doing the assignment for you, but I can't explain it without an example
To loop through a 2D array you do exactly what you do to loop through a 1D array. Since the column of the grades is always the same you only need to change the index for the row
 ``123`` ``````int total; for(int i = 0; i < students; ++i) total += array[i][1];``````

As you can see, at each cycle you will be adding value array[0][1], array[1][1], array[2][1], etc... to the total of the grades

The array is 2D but you don't iterate in two dimensions. TO do that you would do something like
 ``1234`` ``````for(int i = 0; i < rows; ++i) for(int j = 0; j < columns; ++j) something += array[i][j]; ``````
Topic archived. No new replies allowed.