GPA Revisited Given a student’s transcript, calculate their gpa to the nearest t
ID: 3621005 • Letter: G
Question
GPA RevisitedGiven a student’s transcript, calculate their gpa to the nearest two decimal places. A gpa is calculated by taking the total number of credit hours earned, and then dividing by the number of credits taken. As an example, getting an A out of a 3 credit class is worth 12 credit hours because 4 * 3 is equal to 12. Then 12 can be divided by 3 to get a gpa of 4.0.
Input
The first line will state how many grades are received.
Each line after that will contain two values, each separated by a space. The first value will be a number that says how many credits the class is worth. The second value will be a letter that says how many grades are received.
Output
The output will be the final gpa, formatted and rounded to two decimal places. An answer that is within 0.01 of the correct answer will be accepted due to any roundoff error. Ex. In the sample problem below, an answer of either 3.68 or 3.70 will be accepted.
Sample Run
Input
4
3 A
4 B
3 A
3 A
Output
3.69
Explanation / Answer
please rate - thanks
you didn't specify, but I got the imprecision the input was from a file, message me if it's not, can easily be changed
#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;
int main()
{int hours,tothours=0,points,totpoints=0,i,count=0;
double gpa;
char grade;
ifstream in;
in.open("studentGrades.txt"); //open file
if(in.fail()) //is it ok?
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
in>>count;
cout<<"hours grade ";
for(i=0;i<count;i++)
{in>>hours>>grade;
cout<<hours<<" "<<grade<<endl;
tothours+=hours;
switch(grade)
{
case 'A':points=4;
break;
case 'B':points=3;
break;
case 'C':points=2;
break;
case 'D':points=1;
break;
case 'F':points=0;
break;
}
totpoints+=points*hours;
}
gpa=totpoints/(double)tothours;
cout<<"GPA= "<<setprecision(2)<<fixed<<gpa<<endl;
in.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.