I already have the program for this problem. I need to know the algorithm to it.
ID: 3627479 • Letter: I
Question
I already have the program for this problem. I need to know the algorithm to it. It seems that I am not good with writing the algorithms. so only the algorithm will be great!
For research purposes and to better help students, the admissions office of your local university want to know how well female and male students perform in certain courses. You receive a file that contains female and male students GPA 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.
a. Function openFiles: This function opens the input and out 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 female and male students GPA
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 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&,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.