Question

In: Computer Science

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 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:

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

This should be written in C++ please thank you.

Data must be read from a file

Solutions

Expert Solution

Code is provided as text at last

Sample "Student.dat" file-

Program-

Output-

Code-

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <sstream>


using namespace std;

//Define structure studentType

struct studentType

{

   //Declaring variables

   string studentFirstName;
   string studentMiddleName;
   string studentLastName;
   double testScore;
   char grade;

};

//Functions required

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);

//Define main function

int main()

{

   //Defining in variable

   ifstream in;

   //Open student file

   in.open("student.dat");

   //If file can't be opened

   if (in.fail())

   {

       //Display message

       cout << "file did not open please check it\n";

       //Pause console window

       system("pause");

       //Exit

       system("exit");

   }

   //Define array

   studentType sList[20];

   //Call "getData()"

   getData(in, sList, 20);

   //Call "calculateGrade()"

   calculateGrade(sList, 20);

   //Call "printResult()"

   printResult(sList, 20);

   //Close

   in.close();

   //Pause console window

   system("pause");

   //Return 0

   return 0;

}

//Define getData()

void getData(ifstream& inFile, studentType sList[], int listSize)
{

   //Declare variables

   int n = 0;

   //Loop until list size

   while (n<listSize)

   {

       //Store data
       string lineOfFile = "";
       getline(inFile, lineOfFile);
       istringstream iss(lineOfFile);
       if (lineOfFile != "") {
           int numWord = 1;
           do
           {
               std::string sub;
               iss >> sub;
               if (numWord == 1)
                   sList[n].studentFirstName = sub;
               else if (numWord == 2)
                   sList[n].studentMiddleName = sub;
               else if (numWord == 3)
                   sList[n].studentLastName = sub;
               numWord++;
               //std::cout << "Substring: " << sub << std::endl;
           } while (!iss.eof());

           // means only 2 words were entered... example first and last
           if (numWord == 3) {
               sList[n].studentLastName = sList[n].studentMiddleName;
               sList[n].studentMiddleName = "";
           }

           inFile >> sList[n].testScore;
           //inFile >> sList[n].studentLastName >> sList[n].studentFirstName >> sList[n].testScore;

           //Increment counter

           n++;
       }

   }

}

//Define calculateGrade()

void calculateGrade(studentType sList[], int listSize)

{

   //Declare variable

   int i;

   //Loop until length

   for (i = 0; i<listSize; i++)

       //If score less than 60

       if (sList[i].testScore<60)

           //Set grade

           sList[i].grade = 'F';

   //If score less than 70

       else if (sList[i].testScore<70)

           //Set grade

           sList[i].grade = 'D';

   //If score less than 80

       else if (sList[i].testScore<80)

           //Set grade

           sList[i].grade = 'C';

       //If score less than 90

       else if (sList[i].testScore<90)

           //Set grade

           sList[i].grade = 'B';

       //If score greater than 90

       else

           //Set grade

           sList[i].grade = 'A';

}

//Define highestScore()

int highestScore(const studentType sList[], int listSize)

{

   //Declare variables

   int high = 0, i;

   //Loop until length

   for (i = 0; i<listSize; i++)

       //If high value is less than score

       if (high < sList[i].testScore)

           //Set score as high

           high = sList[i].testScore;

   //Return high

   return high;

}

//Define printResult()

void printResult(const studentType sList[], int listSize)

{

   //Display result

   cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;

   //Declare variables

   string name;

   int high, i;

   //Loop until length

   for (i = 0; i<listSize; i++)

   {

       //Set name

       name = sList[i].studentLastName + " " + sList[i].studentMiddleName + " " + sList[i].studentFirstName;

       //Display message

       cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;

   }

   //Set new line

   cout << endl;

   //Call highestScore()

   high = highestScore(sList, listSize);

   //Display message

   cout << "Highest Test Score: " << high << endl;

   //Display result

   cout << "Students having the highest test score: " << endl;

   //Loop until length

   for (int i = 0; i < listSize; i++)

       //If score is high

       if (sList[i].testScore == high)

           //Display student name

           cout << sList[i].studentLastName << " " << sList[i].studentMiddleName << " " << sList[i].studentFirstName << endl;

}


Related Solutions

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...
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,...
Write a program that reads a file consisting of students’ test scores in the range 0–200....
Write a 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, 200,...
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...
Write a program that asks the user to enter five test scores. The program should display...
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage: This method should accept five test scores as arguments and return the average of the scores. determineGrade: This method should accept a test score as an argument and return a letter grade for the score, based on the following grading scale: Score Letter Grade 90-100...
The Test results of the Business Statistics were published and the scores were as followed Students...
The Test results of the Business Statistics were published and the scores were as followed Students 12 14 10 13 17 12 11 15 Results 5 11 7 8 11 7 6 9 Only 2 decimal places Determine the α (alpha) Coefficient in the regression equation * 4 points Your answer Advise if increasing the number of students to 50 will improve the students’ test scores * 2 points Remain the same Increase Decrease Determine the β Coefficient in 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....
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Instructions: Write a program to calculate students’ average test scores and their grades. You may assume...
Instructions: Write a program to calculate students’ average test scores and their grades. You may assume the following input data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT