Sentinel Value & Float Variables
I want to add a line that will count how many grades are 93 or over and output the count. How could I go about doing so?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
//grade2.cpp
//Class avg. w/ sentinel-controlled rep.
#include <iostream.h>
#include <iomanip.h>
int main()
{
int total, gradeCounter, grade;
float average;
//Initialization
total=0;
gradeCounter=0;
//Processing
cout << "Enter grade, -1 to end: ";
cin >> grade;
while (grade != -1) {
total = total+grade;
gradeCounter=gradeCounter + 1;
cout << "Enter grade, -1 to end: ";
cin >> grade;
}
//Termination
if(gradeCounter != 0) {
average=static_cast< float > (total)/gradeCounter;
cout << "Class average is " << setprecision(1)
<<setiosflags(ios::fixed | ios::showpoint)
<<average << endl;
cout << gradeCounter << " grades were entered.";
}
else
cout << "No grades were entered" << endl;
return 0;
}
|
Last edited on
The line you want is topGradeCounter += (grade >= 93) ? 1 : 0;
1 2 3 4 5 6 7 8 9
|
int topGradeCounter = 0;
...
while (grade != -1) {
total = total+grade;
gradeCounter = gradeCounter + 1;
topGradeCounter += (grade >= 93) ? 1 : 0;
|
nicer code:
1 2 3 4
|
while (grade != -1) {
total += grade;
gradeCounter++;
topGradeCounter += (grade >= 93) ? 1 : 0;
|
Last edited on
Thank you very much.
Topic archived. No new replies allowed.