Calculate the grade point average for a student whose information is stored in a
ID: 3640026 • Letter: C
Question
Calculate the grade point average for a student whose information is stored in a file named prog4Data.txt. The file contains the following information:
line 1: student’s name in format First Name Middle Name (optional) Last Name – names are separated by a blank(s)
2: grades- recorded as uppercase letters without spaces separating them (4 grades). Assume that no grade is an “F”
3: credits- recorded as integers without spaces separating the digits (4 digits)
Calculate the student’s GPA without using any selection statements (if, switch, etc.)
Example I/O
data file named prog4Data.txt contains the following
Lonesome Polecat
ABBC
4323
Your output should appear as follows and should appear both on the monitor and in a file named prog4outPut.txt.
Lonesome Polecat’s GPA = 3.08
Explanation / Answer
Here I've used two arrays in different functions(gpa & gpa_1) to calculate the GPA for each student. I executed this code and it's working well.
#include <fstream>
#include <iostream>
int gpa(int);
int gpa_1(int);
using namespace std;
int main()
{
char FirstName[30], LastName[30],MiddleName[30];
FILE *pFile;
int c,ch,sum=0,gsum=0,gr,p,q;
float grade;
ifstream Students("prog4Data.txt");
Students >> FirstName >>MiddleName>> LastName;
pFile=fopen ("prog4Data.txt","r");
do
{
c=fgetc(pFile);
while (ch==('A' || 'B' || 'C' || 'D' || 'F'))
{
ch = fgetc (pFile);
p=gpa(ch);
q=gpa_1(p);
gr=ch * c;
sum=sum+gr;
}
gsum=gsum+c;
}while(c==(1 || 2 || 3 || 4));
fclose (pFile);
grade=sum/gr;
cout<<FirstName<<MiddleName<<LastName<<"'s"<<"GPA="<<grade;
ofstream Student("prog4outPut.txt", ios::out);
Student << FirstName << MiddleName<<LastName << "'s" <<"GPA="<< grade;
return 0;
}
int gpa(int c)
{
int a[5]={'A','B','C','D','F'};
int i=0;
for(i=0;i<5;i++)
{
if(a[i]==c)
return i;
else
return 0;
}
}
int gpa_1(int i)
{
int a[]={4,3,2,1,0};
return a[i];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.