Jon P. answered 07/12/15
Tutor
5.0
(173)
Knowledgeable Math, Science, SAT, ACT tutor - Harvard honors grad
In programming, especially C, the devil is in the details. There are a couple of things that aren't clear.
1. What is n? Does the program know that ahead of time? Does the teacher enter that into the program from the keyboard, or does the program figure it out?
2. Where is the data? Is it in a table that is coded into the program? Is it in a file that is read while the program is executed? Is it entered from the keyboard by the teacher while the program is executed?
3. Have you learned about type casting yet?
4. What does "average" mean? The exact floating point average? The floating point average rounded to the nearest integer? Etc.
The details of the code depend on the answers to each question.
So let's assume 1) that n is coded into the program, 2) that the scores are in a fixed size table that is part of the code, 3) that you know about casting, and 4) that the average will be the decimal average rounded to the nearest integer. Then you could do the following:
#define N ...
int scores[2,N];
int sum[2];
int average[2];
for (i=0; i<2; i++)
{
sum[i] = 0;
for (j=0; j<N; j++)
{
sum[i] += score[i,j];
}
/* Cast the integers sum and n to float to do an exact division, then round to the nearest integer, then cast back to int */
average[i] = (int)roundf ((float)sum[i] / (float)n)
}
average now contains the averages for the two tests, and it can be printed or displayed in some other way