For research purposes and to better help students, the admissions office of your
ID: 3630845 • Letter: F
Question
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 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:a. Function openFiles: opens input and output files. Sets the output of the floating-point numbers to two decimal places in a fixed format with a decimal point and trailing zeros.
b. Function initialize: initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.
c. Function sumGrades: Finds the sum of the female and male students GPA.
d. Function averageGrade: finds the average GPA for female and male students.
e. Function printResults: outputs the relevant results.
f. No global variables. Use parameters to pass information in and out of functions.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int openFiles(ifstream&, ofstream&);
void initialize(int&,double&);
void sumGrades(double&,double,int&);
double averageGrade(double,int);
void printResult(int,double,string,ofstream&);
int main(void)
{ ifstream in;
ofstream out;
int countFemale, countMale;
double sumFemaleGpa, sumMaleGpa,gpa;
char gend;
if(openFiles(in,out)!=0)
{system("pause");
return 0;
}
initialize(countFemale,sumFemaleGpa);
initialize(countMale,sumMaleGpa);
in>>gend;
while(in)
{in>>gpa;
if(gend=='m')
sumGrades(sumMaleGpa,gpa, countMale);
else
sumGrades(sumFemaleGpa,gpa, countFemale);
in>>gend;
}
printResult(countMale,sumMaleGpa, "males",out);
printResult(countFemale,sumFemaleGpa,"females",out);
in.close();
out.close();
system("pause");
return 0;
}
int openFiles(ifstream& in, ofstream& out)
{in.open("input.txt"); //open file
if(in.fail()) //is it ok?
{ cout<<"input file did not open please check it ";
return 1;
}
out.open("output.txt"); //open file
out<<setprecision(2)<<fixed<<showpoint;
}
void initialize(int& c,double& s)
{c=0;
s=0;
}
void sumGrades(double& s,double g,int& c)
{c++;
s+=g;
}
double averageGrade(double s,int c)
{return s/c;
}
void printResult(int c,double s,string mess,ofstream& out)
{out<<"The average of the "<<c<<" "<<mess<<" is "<<averageGrade(s,c)<<endl;
}