53421 Hing 3 A C B 23764 John 3 D D C 18327 Jassica 3 A A C 19372 Amber 4 A B B
ID: 3790077 • Letter: 5
Question
53421 Hing 3 A C B
23764 John 3 D D C
18327 Jassica 3 A A C
19372 Amber 4 A B B C
24364 San 3 A C B
01938 Preet 3 C C B
38472 Deepak 4 D C C B
23941 Bay 3 A A A
01392 Angilica 4 B B C A
32847 Harry 3 F D F
Write a c++ program that reads in the above data file and:
Prints (on a single line) each student's id, name, and GPA (assume all courses are 3 credits). If the student's GPA is below 2.0, start the line off with *** Probation ***.
After all students have been input (i.e., end of file has been reached):
Prints out the number of students processed and the average GPA
Prints out the name and GPA of the students with the lowest and highest GPA's
Explanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <limits.h>
using namespace std;
int main()
{
ifstream inFile;
// Open a file
inFile.open("input.txt");
if (inFile.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
return 1;
}
string id, name;
int gradeCount;
char grade;
double max = INT_MIN;
double min = INT_MAX;
string minName, maxName;
int total = 0;
double score;
double avgGpa = 0;
while (true)
{
double GPA = 0.0;
inFile >> id >> name >> gradeCount;
for (int i = 0; i < gradeCount; ++i)
{
inFile >> grade;
if(grade == 'A')
score = 5.0;
else if(grade == 'B')
score = 4.0;
else if(grade == 'C')
score = 3.0;
else if(grade == 'D')
score = 2.0;
else
score = 1.0;
GPA = GPA + score;
}
GPA = GPA/gradeCount;
avgGpa = avgGpa + GPA;
if(GPA < min)
{
min = GPA;
minName = name;
}
if(GPA > max)
{
max = GPA;
maxName = name;
}
if(GPA < 2.0)
{
cout << " *** Probation ***";
}
cout << " ID: " << id << " Name: " << name << " GPA: " << GPA << endl;
total++;
if(total == 10)
break;
if(inFile.eof())
break;
}
avgGpa = avgGpa/total;
cout << " Total students processed: " << total << endl;
cout << "Avergae GPA: " << avgGpa << endl;
cout << " Lowest GPA: Name: " << minName << " GPA: " << min << endl;
cout << " Highest GPA: Name: " << maxName << " GPA: " << max << endl; return 0;
}
/*
input.txt
53421 Hing 3 A C B
23764 John 3 D D C
18327 Jassica 3 A A C
19372 Amber 4 A B B C
24364 San 3 A C B
01938 Preet 3 C C B
38472 Deepak 4 D C C B
23941 Bay 3 A A A
01392 Angilica 4 B B C A
32847 Harry 3 F D F
output:
ID: 53421
Name: Hing
GPA: 4
ID: 23764
Name: John
GPA: 2.33333
ID: 18327
Name: Jassica
GPA: 4.33333
ID: 19372
Name: Amber
GPA: 4
ID: 24364
Name: San
GPA: 4
ID: 01938
Name: Preet
GPA: 3.33333
ID: 38472
Name: Deepak
GPA: 3
ID: 23941
Name: Bay
GPA: 5
ID: 01392
Name: Angilica
GPA: 4
*** Probation ***
ID: 32847
Name: Harry
GPA: 1.33333
Total students processed: 10
Avergae GPA: 3.53333
Lowest GPA:
Name: Harry
GPA: 1.33333
Highest GPA:
Name: Bay
GPA: 5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.