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

Write a complete C++ program that solves the following problem. Give your source

ID: 3538298 • Letter: W

Question

Write a complete C++ program that solves the following problem. Give your source file a meaningful name, such as gpa.cpp.

The admissions office at a college wants to know how well male and female students perform in their studies. You are given a text file with the structure shown below - each line in the file contains a character code for gender followed by a gpa value. The number of values in the file is unknown, but an x character is provided at the end of the file to act as a sentinel value. Write a program that calculates the average gpa for male students and the average gpa for female students. (If a character other than m or f is entered in the file, it may be ignored). You should note that it is possible to have a class of all male or all female students, so your code should account for that case (do not perform division by 0!).

For your input, create the following text file using notepad. Note that the last line of the file must contain a sentinel value of %u2018x%u2019 to indicate the end of the file.

f           3.5

f           4.0

m         3.91

f           2.20

m         3.01

f           2.97

m         3.17

m         3.54

m         1.97

m         3.75

f           2.5

f           3.85

m         4.0

m         2.20

m         3.5

f           2.0

x

You must use a standard sentinel-controlled while loop in your solution.

Direct your output to the console window. Send your name to the output window first, then format your output as follows:

Average GPA %u2013 Female:     x.xx

Average GPA %u2013 Male:          x.xx

Explanation / Answer

//assumes input file is called 'input.txt'

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

int main()

{

int m=0,f=0;

double msum=0,fsum=0,gpa;

char c=' ';

ifstream myfile("input.txt");

if (myfile.is_open())

{

while(c!='x')

{

myfile>>c;

myfile>>gpa;

if(c=='m')

{ m++; msum=msum+gpa; }

if(c=='f')

{ f++; fsum=fsum+gpa; }

}

}

double avgm,avgf;

if(m==0) { avgm=0; }

else { avgm=msum/m; }

  

if(f==0) { avgf=0; }

else { avgf=fsum/f; }

  

cout<<fixed<<"Average GPA - Female: "<<setprecision(2)<<avgf<<endl;

cout<<fixed<<"Average GPA - Male: "<<setprecision(2)<<avgm<<endl;

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote