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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace 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;
}
Also try showing some output to make the program more user friendly.
Why do you have this line of code:
1 2
for (int i=1;i<=n;i++)
{
Shouldn't your algorithm be
//headers
//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
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:
#include <iostream>
usingnamespace 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 = newfloat [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<MAX_STUDENTS; i++)
{
cout << "\nStudent " << i << ":\n";
average = 0; //clear the average for the next student
//loop for 3 student grades
for (int j = 0; j<3; j++)
{
cout << "Grade " << j << ":\n";
cin >> 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<MAX_STUDENTS; i++)
ClassAverage += StudentGrades[i];
//now calculate the average
ClassAverage /= MAX_STUDENTS;
//display result
cout << "\nThe class average is:\n" << ClassAverage << "\n";
system("pause");
return 0;
}
Notice that I had to dynamically allocate the array to enable the user to enter the number of students : float *StudentGrades = newfloat [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 :)