Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming problem. Please include comments. For research purposes and to b

ID: 3677214 • Letter: C

Question

C++ programming problem. Please include comments.

For research purposes and to better help students, the admissions office of your local university wants to know how well female and male students perform in certain courses. You receive a file (ECPart1Data.txt) that contains female and male student GPAs for certain courses. Due to confidentiality, the letter code f is used for female students and m for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions:

Function openFiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.
Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.
Function sumGrades: This function finds the sum of the female and male students’ GPAs.
Function averageGrade: This function finds the average GPA for female and male students.
Function printResults: This function outputs the relevant results.


There can be no global variables. Use the appropriate parameters to pass information in and out of function

The following is what the data file consists of: f 3.40 f 4.00 m 3.56 m 3.80 f 2.30 f 3.95 m 3.90 m 4.00 m 2.00 f 4.00 f 2.80 m 3.70 m 2.98 f 3.89 m 4.00 f 3.90 m 1.90 m 2.90 f 1.50 f 2.67 m 3.80 m 2.35 f 2.90 f 3.70 f 4.00 m 3.78 m 4.00 f 3.9
8

Explanation / Answer

Please find the code below :

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;


ifstream& openFiles(){
ifstream myfile ("ECPart1Data.txt");
return myfile;
}

void initialize(int countFemale,int countMale,double sumFemaleGPA,double sumMaleGPA){
countFemale=0,countMale=0;
sumFemaleGPA=0,sumMaleGPA=0;
}

void sumGrades(string gender,double gpa,double sumFemaleGPA,double sumMaleGPA,int countFemale,int countMale){
if(gender=="f") {
sumFemaleGPA = sumFemaleGPA + gpa;
countFemale++;
}else{
sumMaleGPA = sumMaleGPA + gpa;
countMale++;
}
}

void printResults(double m, double f){
cout << "Averge gpa of male :" << m << endl;
cout << "Averge gpa of female :" << f << endl;
}

void averageGrade(double sumFemaleGPA,double sumMaleGPA,int countFemale,int countMale){
double m = sumMaleGPA/countMale;
double f = sumFemaleGPA/countFemale;
printResults(m,f);
}


int main () {
ifstream& myfile=openFiles();
int countFemale,countMale;
double sumFemaleGPA,sumMaleGPA;
initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
string line;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
istringstream iss(line);
string s,g;
int i=0;
while ( getline( iss, s, ' ' ) ) {
  
if(i==0){
g=s;
continue;
}
char* endptr;
double gpa = strtod(s.c_str(), &endptr);
sumGrades(g,gpa,sumFemaleGPA,sumMaleGPA,countFemale,countMale);
i++;
}
  
}
averageGrade(sumFemaleGPA,sumMaleGPA,countFemale,countMale);
myfile.close();
}

else cout << "Unable to open file";

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote