Please anybody can walk me to step by step work to program in C++ (Problem 6, ch
ID: 3631270 • Letter: P
Question
Please anybody can walk me to step by step work to program in C++ (Problem 6, chapter 7, from C++ Programming: From problem analysis to program design, 5 edition by D. S Malik page 429). Thank you in advance.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: 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.
b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.
c. Function sumGrades: This function finds the sum of the female and male students’ GPAs.
d. Function averageGrade: This function finds the average GPA for female and male students.
e. Function printResults: This function outputs the relevant results.
f. There can be no global variables. Use the appropriate 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&,int&,double&,double&,double&,double&);
void sumGrades(ifstream&, ofstream&,int&,int&,double&, double&);
void averageGrade(int,int,double,double,double&,double&);
void printResults(ofstream&,double,double);
int main()
{int females,males;
double gpaSumF,gpaSumM,gpaAvgF,gpaAvgM;
ifstream in;
ofstream out;
if(openFiles(in,out)==0)
{initialize(females,males,gpaSumF,gpaSumM,gpaAvgF,gpaAvgM);
sumGrades(in,out,females,males,gpaSumF,gpaSumM);
averageGrade(females,males,gpaSumF,gpaSumM,gpaAvgF,gpaAvgM);
printResults(out,gpaAvgF,gpaAvgM);
}
in.close();
out.close();
return 0;
}
int openFiles(ifstream& in,ofstream& out)
{char filename[30];
cout<<"what is the name of the input file you are using? ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
return 1;
}
cout<<"what is the name of the output file you are using? ";
cin>>filename;
out.open(filename);
return 0;
}
void initialize(int& females,int& males,double& SF,double& SM,double& avgFgpa,
double& avgMgpa)
{females=0;
males=0;
SF=0;
SM=0;
avgFgpa=-1;
avgMgpa=-1;
}
void sumGrades(ifstream& in,ofstream& out,int& females,
int& males,double& SF,double& SM)
{char gender;
double gpa;
in>>gender;
out<<"gender gpa ";
while(in)
{in>>gpa;
out<<gender<<" "<<setprecision(2)<<fixed<<gpa;
if(gender=='f')
{females++;
SF=SF+gpa;
}
else if(gender=='m')
{males++;
SM=SM+gpa;
}
else
out<<" "<<gender<<" unrecognized ";
out<<endl;
in>>gender;
}
}
void averageGrade(int females,int males,double SF,double SM,double& avgFgpa,double& avgMgpa)
{if(females>0)
avgFgpa=SF/females;
if(males>0)
avgMgpa=SM/males;
}
void printResults(ofstream& out,double avgFgpa, double avgMgpa)
{out<<"Average female gpa: "<<setprecision(2)<<fixed<<avgFgpa<<endl;
out<<"Average male gpa: "<<setprecision(2)<<fixed<<avgMgpa<<endl;
out<<"**NOTE** average of -1 means there was no data for that gender student ";
}