Question

C++ Write a program that reads the number of students in the class, then reads each...

C++

Write a program that reads the number of students in the class, then reads each student's name and his/her test score.

Print out each student name, test score and grade, and the average score of the whole class.

The program must use vectors and loops for the implementation.

The grades are determined as follows: A >= 90; 80 <= B < 90; 70 <= C < 80; 60 <= D < 70; F < 60.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
 #include<fstream>
 #include<string>
 #include<iomanip>
 using namespace std;


 const int MAX = 50;
 // a function called GetData to read and store data into two arrays,
 void GetData(ifstream& infile,string name[],int scores[][5],int& n)
 {
    n = 0;
    int i=0;
    int j=0;
    while(!infile.eof())
    {
    infile >> name[i];
    for(int j=0; j<5; j++)
    infile >> scores[i][j];
    i++;
    }
    n = i;
 }

 char determineGrade(double avg)
 {
 if(avg>=90 && avg<=100)
 return 'A';
 else if(avg>=80 && avg<=89)
 return 'B';
 if(avg>=70 && avg<=79)
 return 'C';
 if(avg>=60 && avg<=69)
 return 'D';
 if(avg>=50 && avg<=59)
 return 'F';
 }

 // a function called Average that is used to calculate the average test score and grade,
 void Average(int a[][5],char grade[],double avg[],int no_of_students)
 {
    for(int i=0; i<no_of_students; i++)
    {
    double sum =0;
        for(int j=0; j<5; j++)
        sum+= a[i][j];
    avg[i] = sum/static_cast<double> (5);
    grade[i] = determineGrade(avg[i]);
    }
 }

 // function called PrintResults to output the results.
 void PrintResults(string name[],double avg[],int scores[][5],char grade[],int n)
 {
 for(int i=0; i<n; i++){
 cout << left << setw(10)<< name[i];
    for(int k=0; k<5; k++)
        cout << right << setw(8) << scores[i][k];
    cout << endl;
 }
 cout << setw(8) <<"Average ";
 for(int i=0; i<n; i++)
     cout << setw(5) << avg[i];
     cout << endl;
 }

 int main()
 {
 // a one-dimensional array to store the students� names,
 string name[MAX];
 // a (parallel) two-dimensional array to store the test scores,
 int scores[MAX][5];
 // a parallel one-dimensional array to store grades.
 char grade[MAX];
 int no_of_students;
 double avg[MAX];

 ifstream infile("StudentData.txt");

 if(!infile)
 {
    cout <<"unable to open file.so exiting from program" << endl;
    return 0;
 }

 GetData(infile, name, scores, no_of_students);
 infile.close();
 Average(scores, grade, avg, no_of_students);
 PrintResults(name,avg,scores,grade,no_of_students);


 return 0;
 }
Add a comment
Know the answer?
Add Answer to:
C++ Write a program that reads the number of students in the class, then reads each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • please use the c language Assignment 12 The program to write in this assignment is a...

    please use the c language Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 scores 201 1710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student id...

  • in C porgraming . use #include〈stdio.h〉 #include〈stdlib.h〉 Assignment 12 The program to write in this assignment...

    in C porgraming . use #include〈stdio.h〉 #include〈stdlib.h〉 Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and scores are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 2011710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student_id,...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. 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. Finally, the program outputs the total number of students, each score range...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • In C Langage Write a grading program for a class with the following grading policies:- a. There are two quizzes, each gr...

    In C Langage Write a grading program for a class with the following grading policies:- a. There are two quizzes, each graded on the basis of 10 points. b. There is one midterm exam and one final exam, each graded on the basis of 100 points. c. The final exam counts for 50 percent of the grade, the midterm counts for 25 percent and the two quizzes together count for a total of 25 percent. Grading system is as follows:-...

  • SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes...

    SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array....

  • First, create two inputs that can be stored in variables consisting of a student's name. When...

    First, create two inputs that can be stored in variables consisting of a student's name. When the program begins ask "Who is the first student?" followed by "Who is the second student?" You only have to have two students for this program. Ask the user for the scores for the last three test grades for each of the students .   After the user has entered both names, and three scores for each, the program should display the following as output:...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT