Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am needing to use the pass by reference in this program, but do not know how t

ID: 3627513 • Letter: I

Question

I am needing to use the pass by reference in this program, but do not know how to change it over from the global variables. I do not want any global variables in this program. Can someone possibly change it over for me without changing the program functionality?

#include <iostream>
#include <fstream>
using namespace std;

//Variables
int countMale, countFemale;
float sumMaleGPA, sumFemaleGPA;
float avgMaleGPA, avgFemaleGPA;

//Open input and output files
void openFiles (ifstream &inFile, ofstream &outFile)

{
//Get information from input file
inFile.open ("Ch7_Ex6Data.txt");
if (inFile.fail())
{
  cout <<"Can not find the file input.txt";
  cout <<"Exiting the program...";
  system ("pause");
  exit(0);
}

//Opens the output file
outFile.open ("Ch7_Ex6out.txt");

  //Set precision
outFile.setf(ios::fixed,ios::floatfield);
outFile.precision (2);
//Output to console
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);

}
void initialize()//Initializing Variables
{
countMale=0;
countFemale=0;
sumMaleGPA=0.0;
sumFemaleGPA=0.0;
avgMaleGPA=0.0;
avgFemaleGPA=0.0;
}
//Sum of the student grades
void sumGrades (ifstream &inFile)
{
char gender;
double gpa;
while (!inFile.eof())

{
  inFile>>gender;

  inFile>>gpa;
  if(gender=='M'||gender=='m')
   
  {
   sumMaleGPA += gpa;
   countMale++;
  }
  else
  {

   //Initialize Variables
   sumFemaleGPA += gpa;
   countFemale++;
  }
}

//Close input file
inFile.close();
}
//Find average student grades
void averageGrade()
{
avgMaleGPA=sumMaleGPA/countMale;
avgFemaleGPA=sumFemaleGPA/countFemale;
}
//Print

void printResults(ofstream &outFile)
{
ifstream infile;
infile.open("Ch7_Ex6Data.txt");
double N;
char ch;
outFile<<"Processing grades."<<endl;
while(!infile.eof())
{
infile>>ch>>N;
outFile<<ch<<" "<<N<<endl;

}

outFile<<"Sum female GPA =       "<<sumFemaleGPA<<endl;
cout<<"Sum female GPA =          "<<sumFemaleGPA<<endl;
outFile<<"Sum male GPA =         "<<sumMaleGPA<<endl;
cout<<"Sum male GPA =            "<<sumMaleGPA<<endl;
outFile<<"Female count =          "<<countFemale<<endl;
cout<<"Female count =             "<<countFemale<<endl;
outFile<<"Male count =            "<<countMale<<endl;
cout<<"Male count =               "<<countMale<<endl;
outFile<<"Average female GPA =    "<<avgFemaleGPA<<endl;
cout<<"Average female GPA =       "<<avgFemaleGPA<<endl;
outFile<<"Average male GPA =      "<<avgMaleGPA<<endl<<endl;
cout<<"Average male GPA =         "<<avgMaleGPA<<endl<<endl;


//Close output file
outFile.close ();
}
int main()
{
//input file
ifstream inFile;
//output file
ofstream outFile;

//Calls
openFiles(inFile,outFile);
initialize();
sumGrades(inFile);
averageGrade();
printResults(outFile);
system ("pause");
return 0;
}

Explanation / Answer

put the variables in main use and & in the function call when you want to change the variable here is the code #include #include using namespace std; //Open input and output files void openFiles (ifstream &inFile, ofstream &outFile) { //Get information from input file inFile.open ("Ch7_Ex6Data.txt"); if (inFile.fail()) { cout gpa; if(gender=='M'||gender=='m') { sumMaleGPA += gpa; countMale++; } else { //Initialize Variables sumFemaleGPA += gpa; countFemale++; } } //Close input file inFile.close(); } //Find average student grades void averageGrade(int &countMale, int &countFemale, float &sumMaleGPA, float &sumFemaleGPA, float &avgMaleGPA, float &avgFemaleGPA) { avgMaleGPA=sumMaleGPA/countMale; avgFemaleGPA=sumFemaleGPA/countFemale; } //Print void printResults(ofstream &outFile, int countMale, int countFemale, float sumMaleGPA, float sumFemaleGPA, float avgMaleGPA, float avgFemaleGPA) { ifstream infile; infile.open("Ch7_Ex6Data.txt"); double N; char ch; outFileN; outFile