Write a C++ program that will read names followed by single test grade. The very
ID: 664095 • Letter: W
Question
Write a C++ program that will read names followed by single test grade. The very first line of the file has a single number which represents the max number of points to make on a test. The name is listed first followed by a grade or the points made on a test. Have your program read in the name and grade and print out the name and the percentage grade the student made on the test. (grade/max=%). Print out the list of names and their grade in sorted order sorted by grade. You are to list the sorted grades from the highest grade to the lowest.
You are to use a link list for the data structure.
Example input
300
jones 289
luewey 266
Or-less 178
Example output (Sorted): (Example is not accurate)
Jones 94
Luewey 90
Or-less 83
Input File:
Explanation / Answer
Source code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
struct StudInfo {
string Name;
float marks_obtained;
StudInfo *next;
StudInfo (string name, float marks)
{
Name = name;
marks_obtained = marks;
next = NULL;
}
};
const char *filename = "grades.txt";
int readGradesfile(StudInfo* &start) {
fstream infile;
StudInfo *si;
string name;
float grades;
int num=0;
float max_marks;
infile.open(filename, fstream::in);
if (infile.is_open()) {
infile>>max_marks;
cout<<max_marks;
while (!infile.eof()) {
infile >> name >> grades;
if (start == NULL)
start = new StudInfo(name, grades);
else {
si = start;
while (si->next != NULL) si = si->next; //search the last node
si->next = new StudInfo(name, grades);
}
num++;
}
infile.close();
return num;
}
else return -1; //file open error
}
int main(void) {
StudInfo *pbs, *temp, *head = NULL;
float scr = -1.0;
if (readGradesfile(head) < 0)
cout << "input file " << filename << " not found or couldn't be read" << endl;
else {
temp = head;
while (temp != NULL) {
//float perc=temp->marks_obtained/max_marks;
cout << temp->Name << " " << temp->marks_obtained << endl;
if (temp->marks_obtained > scr) {
scr = temp->marks_obtained;
pbs = temp;
}
temp = temp->next;
}
cout << endl << "Student with the highest score is "
<< pbs->Name << " " << pbs->marks_obtained << endl;
}
//free the allocated memory
while (head != NULL){
temp = head;
head = head->next;
delete temp;
}
system("pause");
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.