I have to find the average of n students (for 3 marks) and then find the average of the class..I wrote the code until a certain point but I can't find the right way to find the total average of n students in class.

 ``123456789101112131415161718`` `````` #include using namespace std; int main () { int n,m1,m2,m3,sum,average,t_sum; float t_average; cin >> n; for (int i=1;i<=n;i++) { cin >> m1 >> m2 >> m3; sum=m1+m2+m3; average=sum/3; t_sum=+average; t_average=(t_sum)/n; } cout << " " << t_average; return 0; }``````
Shouldn't all your numbers be floats?

Also try showing some output to make the program more user friendly.

Why do you have this line of code:

 ``12`` `````` for (int i=1;i<=n;i++) {``````

//namespace std
//main declaration
//floats dec.
//output text explaining what to be entered
//cin >> n;
//output more text
//cin >> the 3 marks
//do your average calculations for average
//get each of the scores for n
//average those
//print them

If i am missing something please just tell me
 `` `` ``for (int i=1;i<=n;i++)``

this line of code is the loop condition for n students..is it necessary or not?
secondly can you explain what do you mean scores for n? (if you understood the number of students will be inputed so that's why I can't find the general algorithm of finding the total average) :/
If I understood correctly, you want to ask the user to enter 3 grades for each student, and calculate the average for that student. then you want to calculate the average of all the students together?

If that so, the easiest way is using an array of floats representing each student average, and averaging the elements in the array after all grades have being put in:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include using namespace std; int main () { //amount of students int MAX_STUDENTS = 0; cout << "How many students are in the class? "; cin >> MAX_STUDENTS; //Array representing each students' average float *StudentGrades = new float [MAX_STUDENTS]; //variables to hold grade and average for a single student float Grade,average; Grade = average = 0; //intialize the variables //main loop for student grade input for (int i = 0; i> Grade; average += Grade; } //now calculate the average average /= 3; // same as: average = average/3 //now store the students' average StudentGrades[i] = average; } //now average out all the grades for the class float ClassAverage = 0; for (int i = 0; i

Notice that I had to dynamically allocate the array to enable the user to enter the number of students :
` float *StudentGrades = new float [MAX_STUDENTS];`
That's because regular arrays need to have a constant number of elements...

Also notice i used:
`system("pause");`
that's only because this is just a tutorial code. If you're writing a project, don't use it! It's a terrible practice..

If something is not clear, or i got your question wrong, tell me. Hope it helps :)
Last edited on
Topic archived. No new replies allowed.