For research purposes and to better help students, the admissions office of the
ID: 3624404 • Letter: F
Question
For research purposes and to better help students, the admissions office of the university wants to know how well female and male students perform in certain courses.
Due to confidentiality, the letter f is for females and m for males. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries is unknown.
-Write a program that computes and outputs the average GPA for both female and male students.
A two decimal point and should contain this functions:
-Function openFiles: This file opens the input andoutput files, and sets the output of the floating-point numbers at 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 females an male students GPAs.
-Function averageGrade: This function finds the average GPA for female and male students.
-Function printResult: This function should write the student counts; sum of the student GPAs, and the average GPAs for both female and male students.
*There can be no global variables. Use the appropiate 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.