please help! I\'m in intro to c++ programming and do not understand this problem
ID: 3799844 • Letter: P
Question
please help! I'm in intro to c++ programming and do not understand this problem. does not need to be intricate, as this is an introduction course..please help.thanks
Explanation / Answer
//C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
using namespace std;
int main()
{
int totalMale = 0;
int totalFemale = 0;
int totalStudents = 0;
double avgGPA = 0, avgGPAmale = 0, avgGPAfemale = 0;
char gender;
double gpa;
while(true)
{
cout << "Enter gender(M/F), q to quit: ";
cin >> gender;
if(gender == 'M')
{
cout << "Enter gpa of male student(0.0-4.0): ";
cin >> gpa;
totalMale++;
avgGPAmale = avgGPAmale + gpa;
avgGPA = avgGPA + gpa;
totalStudents++;
}
else if(gender == 'F')
{
cout << "Enter gpa of female student(0.0-4.0): ";
cin >> gpa;
totalFemale++;
avgGPAfemale = avgGPAfemale + gpa;
avgGPA = avgGPA + gpa;
totalStudents++;
}
else if(gender == 'q')
{
break;
}
else
cout << "Invalid Input ";
cout << endl;
}
avgGPAmale = avgGPAmale / totalMale;
avgGPAfemale = avgGPAfemale / totalFemale;
avgGPA = avgGPA / totalStudents;
cout << " Average GPA of male students " << avgGPAmale << endl;
cout << "Average GPA of female students " << avgGPAfemale << endl;
cout << "Average GPA of all students " << avgGPA << endl;
return 0;
}
/*
output:
Enter gender(M/F), q to quit: M
Enter gpa of male student(0.0-4.0): 3.5
Enter gender(M/F), q to quit: F
Enter gpa of female student(0.0-4.0): 2.3
Enter gender(M/F), q to quit: M
Enter gpa of male student(0.0-4.0): 2.3
Enter gender(M/F), q to quit: M
Enter gpa of male student(0.0-4.0): 4.0
Enter gender(M/F), q to quit: F
Enter gpa of female student(0.0-4.0): 2.1
Enter gender(M/F), q to quit: F
Enter gpa of female student(0.0-4.0): 3.4
Enter gender(M/F), q to quit: M
Enter gpa of male student(0.0-4.0): 2
Enter gender(M/F), q to quit: F
Enter gpa of female student(0.0-4.0): 3
Enter gender(M/F), q to quit: q
Average GPA of male students 2.95
Average GPA of female students 2.7
Average GPA of all students 2.825
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.