Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that asks the user for the number of students in the class. The program then asks the user for the grade of each student and computes/displays the average grade in the class. You are required to use a while loop.


C
#include <iostream>
using namespace std;
int main (){
	int studentNum;
	int sudentGrade;
	int avarageGrade;
	int countStudent = 0;
	cout << "Please enter the number of student = ";
	cin >> studentNum;
	while (studentNum>countStudent)
	{
		cout << "Please enter the grade = ";
		cin >> sudentGrade;

	}
	system("pause");
	return 0;
}
Posted
Updated 20-Oct-15 23:15pm
v3
Comments
Matt T Heffron 20-Oct-15 20:52pm    
Clearly this is homework.
You should at least try to do it yourself.
If necessary, come back and ask a question afterwards.
Assam ALzookery 20-Oct-15 20:56pm    
i really did but i have hard time to calculate the average of the grades

#include <iostream>
using namespace std;
int main (){
int studentNum;
int sudentGrade;
int avarageGrade;
int countStudent = 0;
cout << "Please enter the number of student = ";
cin >> studentNum;
while (studentNum>countStudent)
{
cout << "Please enter the grade = ";
cin >> sudentGrade;

}
system("pause");
return 0;
}
Matt T Heffron 20-Oct-15 21:00pm    
Use the "Improve question" above and move this code to there.
Be sure to use the correct formatting (use the "code" button in the editor and select the C++ language.)
Assam ALzookery 20-Oct-15 21:02pm    
i did please try to answer my question if you can
Thank you.
Matt T Heffron 20-Oct-15 21:08pm    
Ok, you should know the definition of average (i.e., the mean). It is the sum of the values divided by the number of values. So after you read in a grade you should add it into your variable holding the average. (You need to increment the countStudent each time or you'll loop forever.) Then after the loop, print out an appropriate string and then the result of dividing the sum of the individual grades by the count.

1 solution

You should sum up the grades of all the students. It can be done incrementally, in the while loop (don't forget to initialize to 0 the grade sum before entering the loop). At the end of the loop compute the average grade value by dividing such sum by the number of the students.
 
Share this answer
 
Comments
Matt T Heffron 21-Oct-15 12:53pm    
Isn't that essentially what I said in my comment above? ;-)
+5
CPallini 21-Oct-15 15:43pm    
Yes, it is :-D
By the way, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900