You are to write a program that will read names followed by single test grade. T
ID: 643790 • Letter: Y
Question
You are to write a 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 and a second list sorted by grade. You are to list the sorted grades from the highest grade to the lowest.
You are to use two links in your link list. One link links the names in sorted ordered and a second link to link the grades in sorted ordered.
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 Data:
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct node{
string name;
int grade;
node *nextn;
node *nextg;
};
void addNameNode(node *&head, node *t){
node *temp = head;
node *prev = NULL;
if(head->name > t->name){
head = t;
t->nextn = temp;
return;
}
while(temp->nextn != NULL){
if(temp->nextn->name > t->name){
t->nextn = temp->nextn;
temp->nextn = t;
return;
}
temp = temp->nextn;
}
temp->nextn = t;
}
void addGradeNode(node *&head, node *t){
node *temp = head;
node *prev = NULL;
if(head->grade < t->grade){
head = t;
t->nextg = temp;
return;
}
while(temp->nextg != NULL){
if(temp->nextg->grade < t->grade){
t->nextg = temp->nextg;
temp->nextg = t;
return;
}
temp = temp->nextg;
}
temp->nextg = t;
}
void addNode(node *&headg, node *&headn, node *t){
if(headg == NULL){
headg = t;
headn = t;
}
else{
addNameNode(headn, t);
addGradeNode(headg, t);
}
}
void printByGrade(node *head, int max){
cout << setw(10) << "Name" << setw(10) << "Grade" << setw(15) << "Percentage" << endl;
while(head != NULL){
cout << setw(10) << head->name << setw(10) << head->grade << setw(15) << setprecision(4) << head->grade * 100.0 / max << endl;
head = head->nextg;
}
}
void printByName(node *head, int max){
cout << setw(10) << "Name" << setw(10) << "Grade" << setw(15) << "Percentage" << endl;
while(head != NULL){
cout << setw(10) << head->name << setw(10) << head->grade << setw(15) << setprecision(4) << head->grade * 100.0 / max << endl;
head = head->nextn;
}
}
int main(){
string fName = "nameWeight.txt";
ifstream in;
//cout << "Enter file name: ";
//cin >> fName;
int max, count = 0;
node *headg = NULL, *headn = NULL;
string name;
int grade;
in.open(fName.c_str());
if(in.is_open()){
in >> max;
while(in >> name){
in >> grade;
node *t = new node;
t->grade = grade;
t->name = name;
t->nextg = NULL;
t->nextn = NULL;
addNode(headg, headn, t);
}
cout << "Sorted by grades " << endl;
printByGrade(headg, max);
cout << "Sorted by Names" << endl;
printByName(headn, max);
}
else{
cout << fName << " is not found in the directory" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.