Question

In: Computer Science

C++ Description: You will write a program that reads students names followed by their final grade....

C++

Description: You will write a program that reads students names followed by their final grade. It will output the names and their grade, followed by the corresponding letter grade. It will also print the name of the students(s) with the highest grade

  • Student data will be stored in a struct called studentType which has 4 members:
    • fName
    • lName
    • score
    • letterGrade
  • Assume there are 20 students
  • Your main() function will only have variable definitions and function calls
    • You MUST have the following functions
      • readData
      • assignGrade
      • findHighest
      • printHighest

Solutions

Expert Solution

Program code to copy:-

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//Declare global constants
const int SIZE = 20;

//Defining structure
struct studentType
{
    string fname;
    string lname;
    double score;
    char letterGrade;
};

//Function prototype
int readData(studentType stud[]);
void assignGrade(studentType stud[], int n);
void printStudentsInfo(studentType stud[], int n);
double findHighest(studentType stud[], int n);
void printHighest(studentType stud[], int n, double highest);

int main()
{
    //Declare array to hold data read from file  
    studentType stud[SIZE];
    //Declare variable to hold number students deatils read from file
    int n;
  
    //Calling function to read data from file into structure array
    n = readData(stud);
  
    //Calling function to determine and assign grade for each student
    assignGrade(stud, n);
  
    //Calling function to print students name, grade and letter grade
    printStudentsInfo(stud, n);
  
    //Calling function to find the highest grade
    double highest = findHighest(stud, n);
  
    //Calling function to print the names of students with highest grade
    printHighest(stud, n, highest);
}

//Function read first name, last name and grade from file into structure array.
//Function takes reference to structure array as parameter and
//returns number of students data read from file.
int readData(studentType stud[])
{
    ifstream fin;
    string filename;
  
    //Prompt and read file name from user
    cout << "Enter the input file name: ";
    cin >> filename;
  
    //Open the file for reading
    fin.open(filename.c_str());
    //Checking whether the file is opened successfully or not
    if(!fin)
    {
        cout << "File failed to open for reading" << endl;
        exit(0);
    }
  
    int i=0;  
    //Loop will be executed till end of file
    while(!fin.eof())
    {
        //Reading each student first name, last name and grade from file into structure array.
        fin >> stud[i].fname >> stud[i].lname >> stud[i].score;
        i++;
    }
    //Closing file
    fin.close();
    //Returns the number students deatils read from file
    return i;
}


//Function to determine and assign grade for each student.
//Function takes two parameters: reference to array of structure and
//number of students.
void assignGrade(studentType stud[], int n)
{
    //determine and assign grade for each student
    for(int i=0; i<n; i++)
    {
        if (stud[i].score >= 90)
                stud[i].letterGrade = 'A';
        else if (stud[i].score >= 80)
                stud[i].letterGrade = 'B';
        else if (stud[i].score >= 70)
                stud[i].letterGrade = 'C';
        else if (stud[i].score >= 60)
                stud[i].letterGrade = 'D';
        else
                stud[i].letterGrade = 'F';
    }
  
}

//Function to print students name, grade and letter grade
//Function takes two parameters: reference to array of structure and
//number of students.
void printStudentsInfo(studentType stud[], int n)
{
    cout << endl;
    cout << left << setw(15) << "First Name" << left << setw(15) << "Last Name"
         << right << setw(7) << "Grade" << right << setw(20) << "Letter-Grade" << endl;
    cout << "-----------------------------------------------------------" << endl;
    cout << setprecision(1) <<fixed;
    for(int i=0; i<n; i++)
    {
        cout << left << setw(15) << stud[i].fname << left << setw(15) << stud[i].lname
             << right << setw(7) << stud[i].score << right << setw(15) << stud[i].letterGrade << endl;
    }
    cout << "-----------------------------------------------------------" << endl;
}

//Function to finds and returns the highest grade.
//Function takes two parameters: reference to array of structure and
//number of students.
double findHighest(studentType stud[], int n)
{
    double max = stud[0].score;
    for(int i=1; i<n; i++)
    {
        if(stud[i].score > max)
            max = stud[i].score;
    }
      
    return max;
}

//Function to print the names of students with highest grade.
//Function takes three parameters: reference to array of structure,
//number of students and highest grade.
void printHighest(studentType stud[], int n, double highest)
{
    cout << endl;
    cout << "Name of students scored highest grade:" << endl;
    cout << "--------------------------------------" << endl;
    for(int i=1; i<n; i++)
    {
        if(stud[i].score == highest)
            cout << stud[i].fname << " " << stud[i].lname << endl;
    }
}

Screenshot of output:-

Sample input file from where student first name, last name and grade read by the program:-

"StudentGrade.txt"

Daryl Dixon 89
Rick Grimes 92
Glenn Rhee 94
Maggie Greene 82
Carl Grimes 75
Carol Peletier 100
Beth Greene 87
Sasha Williams 79
Tara Chambler 81
Rosita Espinosa 90
Eugene Porter 100
Alfalfa Aloysius 40.0
Iker Casillas 65
Mesut Ozil 47
Xabi Alonso 100


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 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...
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
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/C++ program which reads in a list of process names and integer times from...
Write a C/C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read the list until end of transmission (^d). You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer...
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,...
Question: How do I write a program in C# that calculates a student's final grade for...
Question: How do I write a program in C# that calculates a student's final grade for a test with 20 multiple questions using arrays and helper methods? Instructions: (0-incorrect answer, 1-correct answer) If the student answered the question right, add .5 points to the running total. If the student didn’t answer correctly subtract .5 points from the running total. The running total is initialized with 5 points (so the student receives 5 points extra credit). To define the final grade...
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...
c++ class homework Topics If/Else If statement Description Write a program that determines a student’s final...
c++ class homework Topics If/Else If statement Description Write a program that determines a student’s final grade in the course. The course had three tests (100 points each) and four assignments (also 100 points each). All the test scores make up 70% of the grade and all the assignments 30% of the grade. The program asks the user to input one by one each of the test scores and each of the assignment scores. From these scores, it computes the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT