You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Assignment: Write a program to ask the user to enter students' grades and then calculate the total grade.
1. for loop:
#include<iostream>usingnamespacestd;intmain() {
int num_students;
float stu_grade; // student's numeric course gradefloat total_grade = 0;
cout << "How many students are in the class? ";
cin >> num_students;
for (int i = 1; i <= num_students; i++) {
cout << "Please enter the #" << i << " student's numeric course grade: ";
cin >> stu_grade;
total_grade += stu_grade;
}
cout << "The total grade of the class is: " << total_grade << endl;
return0;
}
2. while loop:
#include<iostream>usingnamespacestd;intmain() {
constint SENTINEL_VAL = -1;
float stu_grade; // student's numeric course gradefloat total_grade = 0;
cout << "Type a student's numeric course grade (type -1 if you don't have any more grade): ";
cin >> stu_grade;
while (stu_grade != SENTINEL_VAL) {
total_grade += stu_grade;
cout << "Type a student's numeric course grade (type -1 if you don't have any more grade): ";
cin >> stu_grade;
// total_grade += stu_grade; // should at the beginning, otherwise, the -1 will be add.
}
cout << "The total grade of the class is: " << total_grade << endl;
return0;
}
3. do while loop:
#include<iostream>usingnamespacestd;intmain() {
char user_response;
float stu_grade; // student's numeric course gradefloat total_grade = 0;
do {
cout << "Type a student's numeric course grade: ";
cin >> stu_grade;
total_grade += stu_grade;
cout << "Do you have another grade to type? (Y/N) ";
cin >> user_response;
} while (user_response == 'Y' || user_response == 'y');
cout << "The total grade of the class is: " << total_grade << endl;
return0;
}
The text was updated successfully, but these errors were encountered:
Assignment: Write a program to ask the user to enter students' grades and then calculate the total grade.
1. for loop:
2. while loop:
3. do while loop:
The text was updated successfully, but these errors were encountered: