1. ## Grade program average problem

I've got everything printed an formatted as per the example in the attachment, but when the program runs, the values for average are all incorrect. Any ideas/solutions are welcome and appreciated

Code:
```#include <stdio.h>
#include <stdlib.h>

{
if(x>=90)
printf("A");
else if(x>=80)
printf("B");
else if(x>=70)
printf("C");
else if(x>=60)
printf("D");
else
printf("F");
}

int main()
{
double total[15];
double average[7];
double sum=0;
int a;
printf("\t HW1\t HW2\t Quiz1\t Quiz2\tMidterm\tFinal\t Total\t Grade\n"); //: Header://

for(int i=0; i<15; i++)
{
for (int j=0; j<6; j++)
{
grade[i][j] = 50.0 + rand() % 50; //: generates random grade for each element in the 2d array://
sum=0;
sum = sum + grade[i][j] ;
average[j]= sum/15;
}
}

for(int i=0; i<15; i++)
{
printf("\n");
printf("%d\t", i+1); //: prints the student ID number, (line numbers)://
for (int j=0; j<6; j++)
{
}
printf("%6.2f\t  ", total[i]);
}
printf("\nAverage:"); //: Here for the sake of shiowing where I need it formatted://

for(a=0;a<7;a++)
printf("%6.2f\t", average);

printf("\n");
system("pause");
}```

2. Lots of arithmetic problems!

You're dividing inside the inner loop - move it to just below the inner loop.
Also, sum = 0 needs to be moved just before the inner for loop for j, begins. (inside the for i loop, but outside the for j loop.

To average the HW, quizzes, and tests, you'll need to do the index "flip" trick on the inner for loop. Otherwise, you'll make an average for the rows, instead of the column, as you have already seen.

From looking at the code, i thought you were trying to get an average score for each student, but later saw you wanted an average for the class, for each test, etc.

This is completely untested for accuracy, and I have not looked at your assignment at all. The assignment is yours to handle. If you can't describe the programming problem here, I can't help you.

Code:
```#include <stdio.h>
#include <stdlib.h>

{
if(x>=90)
printf("A");
else if(x>=80)
printf("B");
else if(x>=70)
printf("C");
else if(x>=60)
printf("D");
else
printf("F");
}

int main()
{
double total[15];
double average[7];
double sum=0.0;
int i, j, r, c;
printf("\n\t HW1\t HW2\t Quiz1\t Quiz2\tMidterm\tFinal\t Total\t Grade\n"); //: Header://

for(i=0; i<15; i++)
{
sum = 0.0;
for (j=0; j<6; j++)
{
grade[i][j] = 50.0 + rand() % 50; //: generates random grade for each element in the 2d array://
sum = sum + grade[i][j];
}
}
for(i=0; i<15; i++)
{
printf("\n");
printf("%d\t", i+1); //: prints the student ID number, (line numbers)://
for (j=0; j<6; j++)
{
}
printf("%6.2f\t  ", total[i]);
}
printf("\n\nAverage:"); //: Here for the sake of shiowing where I need it formatted://
//c = column, r = row. note how c & r are "flipped"
for(c = 0; c < 6; c++) {
for(r = 0, sum = 0; r < 15; r++) {
sum += grade[r][c];  //  flipped here
//if(c== 6) printf("\t%4.2lf", grade[r][c]); //for debug only
}
average[c] = sum/15;
}
/*
This "flipping" of indexes is very common for scoreboards and
any summation of columns in a table - like here
*/

//average score for all students on 1 HW, Quiz, or Test
for(i=0;i< 6;i++)
printf("%6.2f\t", average[i]);

printf("\n\n\t\t\t     press enter when ready");
i = getchar();
return 0;
}```
That will get you started.

Remember to have a return "something", for every function that has a return type. If you don't want a return, then make it a void function. main() is the exception - it should always return 0 or "EXIT_SUCCESS" on a normal exit, or some other value, depending on the type of failure.

Popular pages Recent additions