i have a program in computing where i have to read a list of students and their
ID: 3695263 • Letter: I
Question
i have a program in computing where i have to read a list of students and their grades from an input file and calculate each studen't average, then copy the information into a new output file. i'm stuck at the moment.the instructions are:
Write a program to compute average grades for a course. The course records are in a single file (named quiz.txt) and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file (named average.txt) will be nearly the same as the data in the input file except that you will print the names as last_name, first_name; each quiz score, and there will be one additional number at the end of each line: the average of the student’s ten quiz scores. The output file must be formatted as described below: (1) The output file should contain a line (or lines) at the beginning of the file providing appropriate column headings. Use formatting statements to make the layout clean and easy to read. (2) first and last names appear together in a left justified column that is 20 characters wide where the last name comes first, then a comma and a space and then the first name. Read each name separately and then put them together into a larger correctly formatted string before trying to output them. (3) Each quiz score should be listed in a right justified column that is 4 characters wide. Note that if a student has fewer than 10 scores (they have missed one or more of the quizzes), your program will need to display the missing score(s) using 0 or empty space(s) for each one. (4) The average should appear in its own right justified column that is 10 characters wide. Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10.
Explanation / Answer
Program: Please follow these coding as shown in below...
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
const int MAX = 50;
// 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';
}
// 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.