Please read carefully and looking for almost perfect! Input file information: Du
ID: 3627796 • Letter: P
Question
Please read carefully and looking for almost perfect!
Input file information:
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
Lance Norman 88
Write a program that reads students' names followed by their test scores. The program should output each students name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.
Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType.
Your program must contain atleast the following functions:
a. A function to read the students data into the array.
b. A function to assign the relevant grade to each student.
c. A function to find the highest testscrore.
d. A function to print the names of the students having the highest test score.
Your program must output each student's name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.
Explanation / Answer
#include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; const int numStudents = 20; //assuming 20 students tookt he test struct studentType { string studentFName; string studentLName; int testScore; char grade; }; //A function to read the students data into the array. void getData(ifstream &inFile, studentType test1[numStudents]) { for(int i = 0; i < numStudents; i++) { inFile >> test1[i].studentFName >> test1[i].studentLName >> test1[i].testScore; //assuming data file will be like <first> <last> <score> } inFile.close (); //closing data file } //A function to assign the relevant grade to each student. void getLetterGrades(studentType test1[numStudents]) { for(int i = 0; i < numStudents; i++) //loop to go thru all the students and figure out the letter grade { //compares the testscore to each 90+, 80-89, 70-79, 60-69, 60- brackets to determine letter grade if (test1[i].testScore < 60) test1[i].grade = 'F'; else if (test1[i].testScore < 70) test1[i].grade = 'D'; else if (test1[i].testScore < 80) test1[i].grade = 'C'; else if (test1[i].testScore < 90) test1[i].grade = 'B'; else test1[i].grade = 'A'; } } //A function to find the highest testscrore. int findHighest(studentType test1[numStudents]) { int top = test1[0].testScore; //sets the highest student to the first element for(int i = 1; i < numStudents; i++) { if(top < test1[i].testScore) top = test1[i].testScore; //if the current element is higher than the current highest then the highest is the current element } return top; //returns the highest scoring student } void printTests(ofstream &outFile, studentType test1[numStudents]) { //align the names to the left cout.setf(ios::left); cout << setw(20) << "Name"; cout.unsetf(ios::left); //align the test headings to the right cout.setf(ios::right); cout << setw(3) << "Score" << setw(8) << "Grade" << endl; //align the names to the left outFile.setf(ios::left); outFile << setw(20) << "Name"; outFile.unsetf(ios::left); //align the test headings to the right outFile.setf(ios::right); outFile << setw(3) << "Score" << setw(8) << "Grade" << endl; for(int i = 0; i < numStudents; i++) { cout.unsetf(ios::right); cout.setf(ios::left); //align the names to the left cout << setw(20) << test1[i].studentLName + ", " + test1[i].studentFName; cout.unsetf(ios::left); cout.setf(ios::right); //align the tests to the right cout << setw(3) << test1[i].testScore << setw(8) << test1[i].grade << endl; outFile.unsetf(ios::right); outFile.setf(ios::left); //align the names to the left outFile << setw(20) << test1[i].studentLName + ", " + test1[i].studentFName; outFile.unsetf(ios::left); outFile.setf(ios::right); //align the tests to the right outFile << setw(3) << test1[i].testScore << setw(8) << test1[i].grade << endl; } } //A function to print the names of the students having the highest test score. void printHighest(ofstream &outFile, studentType test1[numStudents], int highest) { cout << " The highest test score of " << highest << " was earned by: " << endl; //prints out the highest test score outFile << " The highest test score of " << highest << " was earned by: " << endl; //prints out the highest test score for(int i = 0; i < numStudents; i++) { if(test1[i].testScore == highest) { cout << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score outFile << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score } } } int main() { studentType test1[numStudents]; //test scores array made for students ifstream inFile; ofstream outFile; inFile.open("data.txt"); outFile.open("results.txt"); //opens input and output for reading/writing getData(inFile, test1); //fills the array up with names and scores getLetterGrades(test1); //gives a letter grade to each student's score printTests(outFile, test1); //prints the results printHighest(outFile, test1, findHighest(test1)); //prints the highest scorers outFile.close (); //closing output file system("pause"); return 0; } #include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; const int numStudents = 20; //assuming 20 students tookt he test struct studentType { string studentFName; string studentLName; int testScore; char grade; }; //A function to read the students data into the array. void getData(ifstream &inFile, studentType test1[numStudents]) { for(int i = 0; i < numStudents; i++) { inFile >> test1[i].studentFName >> test1[i].studentLName >> test1[i].testScore; //assuming data file will be like <first> <last> <score> } inFile.close (); //closing data file } //A function to assign the relevant grade to each student. void getLetterGrades(studentType test1[numStudents]) { for(int i = 0; i < numStudents; i++) //loop to go thru all the students and figure out the letter grade { //compares the testscore to each 90+, 80-89, 70-79, 60-69, 60- brackets to determine letter grade if (test1[i].testScore < 60) test1[i].grade = 'F'; else if (test1[i].testScore < 70) test1[i].grade = 'D'; else if (test1[i].testScore < 80) test1[i].grade = 'C'; else if (test1[i].testScore < 90) test1[i].grade = 'B'; else test1[i].grade = 'A'; } } //A function to find the highest testscrore. int findHighest(studentType test1[numStudents]) { int top = test1[0].testScore; //sets the highest student to the first element for(int i = 1; i < numStudents; i++) { if(top < test1[i].testScore) top = test1[i].testScore; //if the current element is higher than the current highest then the highest is the current element } return top; //returns the highest scoring student } void printTests(ofstream &outFile, studentType test1[numStudents]) { //align the names to the left cout.setf(ios::left); cout << setw(20) << "Name"; cout.unsetf(ios::left); //align the test headings to the right cout.setf(ios::right); cout << setw(3) << "Score" << setw(8) << "Grade" << endl; //align the names to the left outFile.setf(ios::left); outFile << setw(20) << "Name"; outFile.unsetf(ios::left); //align the test headings to the right outFile.setf(ios::right); outFile << setw(3) << "Score" << setw(8) << "Grade" << endl; for(int i = 0; i < numStudents; i++) { cout.unsetf(ios::right); cout.setf(ios::left); //align the names to the left cout << setw(20) << test1[i].studentLName + ", " + test1[i].studentFName; cout.unsetf(ios::left); cout.setf(ios::right); //align the tests to the right cout << setw(3) << test1[i].testScore << setw(8) << test1[i].grade << endl; outFile.unsetf(ios::right); outFile.setf(ios::left); //align the names to the left outFile << setw(20) << test1[i].studentLName + ", " + test1[i].studentFName; outFile.unsetf(ios::left); outFile.setf(ios::right); //align the tests to the right outFile << setw(3) << test1[i].testScore << setw(8) << test1[i].grade << endl; } } //A function to print the names of the students having the highest test score. void printHighest(ofstream &outFile, studentType test1[numStudents], int highest) { cout << " The highest test score of " << highest << " was earned by: " << endl; //prints out the highest test score outFile << " The highest test score of " << highest << " was earned by: " << endl; //prints out the highest test score for(int i = 0; i < numStudents; i++) { if(test1[i].testScore == highest) { cout << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score outFile << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score } } } int main() { studentType test1[numStudents]; //test scores array made for students ifstream inFile; ofstream outFile; inFile.open("data.txt"); outFile.open("results.txt"); //opens input and output for reading/writing getData(inFile, test1); //fills the array up with names and scores getLetterGrades(test1); //gives a letter grade to each student's score printTests(outFile, test1); //prints the results printHighest(outFile, test1, findHighest(test1)); //prints the highest scorers outFile.close (); //closing output file system("pause"); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.