Function open Files: This function opens the input and output tiles and sets the
ID: 3635470 • Letter: F
Question
Function open Files: This function opens the input and output tiles and sets the output of the floaring-point numbers to two decimal phecs 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: T his function finds the average Gl'A tot female and male students. Function printResults: This function outputs the relevant results. There can be no global variables. Use the appropnate parameter to pas 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.