Question

In: Computer Science

using c++. ALWAYS GRADE MY ANSWERS Write a program that reads students’ names followed by their...

using c++. ALWAYS GRADE MY ANSWERS

Write a program that reads students’ names followed by their test scores. The program should output each student’s 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 at least the following functions:

  • A function to read the students’ data into the array.
  • A function to assign the relevant grade to each student.
  • A function to find the highest test score.
  • 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.

Your program should accept no input and save the output to Ch9_Ex2Out.txt.

HERE IS :Ch9_Ex2Data.txt

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

Solutions

Expert Solution

code:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct studentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[], int listSize);
int main() {
ifstream in;
in.open("Ch9_Ex2Data.txt");
if(in.fail()){
cout<<"file did not open please check it\n";
system("pause");
system("exit");
}
studentType sList[20];
getData(in,sList,20);
calculateGrade(sList,20);
printResult(sList,20);
in.close();
return 0;
}

void getData(ifstream& inFile,studentType sList[],int listSize) {
int n=0;
while(n<listSize) {
inFile>>sList[n].studentLName>>sList[n].studentFName>>sList[n].testScore;
n++;
}
}
void calculateGrade(studentType sList[], int listSize) {
int i;
for(i=0;i<listSize;i++) {
if (sList[i].testScore<60)
sList[i].grade='F';
else if (sList[i].testScore<70)
sList[i].grade='D';
else if (sList[i].testScore<80)
sList[i].grade='C';
else if(sList[i].testScore<90)
sList[i].grade='B';
else
sList[i].grade='A';
}   
}
int highestScore(const studentType sList[], int listSize) {
int high=0,i;
for(i=0;i<listSize;i++)
if (high < sList[i].testScore )
high=sList[i].testScore;
return high;
}

void printResult(const studentType sList[], int listSize) {
ofstream myfile ("CH9_EX2Out.txt");
if (myfile.is_open()){
cout<<left<<setw(30)<<"Student Name"<<right<<setw(10)<<"TestScore"<<right<<setw(7)<<"Grade"<<endl;
myfile<<left<<setw(30)<<"Student Name"<<right<<setw(10)<<"TestScore"<<right<<setw(7)<<"Grade"<<endl;
string name;
int high,i;
for(i=0;i<listSize;i++) {
name=sList[i].studentLName+", "+sList[i].studentFName;
cout<<left<<setw(30)<<name<<right<<setw(10)<<sList[i].testScore<<right<<setw(7)<<sList[i].grade<<endl;
myfile<<left<<setw(30)<<name<<right<<setw(10)<<sList[i].testScore<<right<<setw(7)<<sList[i].grade<<endl;
}
cout<<endl;
high=highestScore(sList, listSize);
cout<<"Highest Test Score: "<<high<<endl;
myfile<<"Highest Test Score: "<<high<<endl;
cout<<"Students having the highest test score: "<<endl;
myfile<<"Students having the highest test score: "<<endl;
for (int i=0; i < listSize; i++)
if (sList[i].testScore==high ) {
myfile<<sList[i].studentLName<<", "<<sList[i].studentFName<<endl;
cout<<sList[i].studentLName<<", "<<sList[i].studentFName<<endl;
}
myfile.close();
cout << "Output also present in CH9_EX2Out.txt" << endl;
}
else {
cout << "Error Occurred, while creating file" << endl;
}
}

OUTPUT:


Related Solutions

Write a program that reads students’ names followed by their test scores. The program should output...
Write a program that reads students’ names followed by their test scores. The program should output each student’s 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...
Write a C++ program that reads a students name followed by 7 numeric grades from a...
Write a C++ program that reads a students name followed by 7 numeric grades from a file.  Compute the average grade after dropping the  lowest grade.   Assign letter grades via this scale .Please show steps in the comments . A 90 – 100 B 80  --89 C 70 –79 D 60 -69 F 0  - 59 Input format: Sam 100 90 87 23 12 67 95 Mary 30 20 90 90 90 90 88 Mark 80 90 80 80 90 87 100 End of file...
using c++. i always grade my answers The variables x, y, z, rate, and hours referred...
using c++. i always grade my answers The variables x, y, z, rate, and hours referred to in the bullets below are the variables of the function main. Each of the functions described must have the appropriate parameters to access these variables. Write the following definitions: Write the definition of the function initialize that initializes x and y to 0 and z to the blank character. Write the definition of the function getHoursRate that prompts the user to input the...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C program that calculates the average grade for a specified number of students from...
Write a C program that calculates the average grade for a specified number of students from each student's test1 and test2 grades. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the test1 grade (grade #1) and test2 grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (test1 and test2). Use a two-dimensional float array to store...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT