In: Computer Science
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays.
Function prototypes:
int readData(ifstream &iFile, int scores[][10], char
names[][30]);
This functions takes the file stream parameter inFile by reference,
after the file is open. It reads from the text file into the names
array and the scores array. It essentially populates the array with
data from the file and returns an int which is the count of the
number of items in the array.
void findLowestEach(int scores[][10], char names[][30], int size);
This function takes the 2 parallel arrays and an int size and prints each students’ names
and lowest score on separate lines.
void findHighestEach(int scores[][10], char names[][30], int size);
This function takes the 2 parallel arrays and an int size and prints each students’ names
and highest score on separate lines.
void calcAverage(int scores[][10], char names[][30], int size); This functions take the 2 parallel arrays and an int size, and calculates the average score of each students and puts it in a new array called average[]. The average array can be a local variable double average[CAP]; Print the students name and average at the end of this function.
int main()should declare two 2-dim arrays, one for students names, char names[CAP][30]; and int scores[CAP][NUM_GRADES]; CAP is an int constant = 100, and NUM_GRADES is an int constant = 10. You should also declare a file stream variable inFile and open the file. Then, main should call readData, and load the data from the text file. Then main should call findLowestEach(.....), and then
findHighestEach(.........), and print the values. Then main should call calcAverage(...) and print the students’ names and their average.
data.txt
Bob 56 67 83 76 84 94 68 86 78 56
John 76 89 95 64 78 34 99 89 104 98
Joe 65 68 89 78 45 69 98 101 99 89
Amy 86 77 89 76 94 54 78 89 78 66
Nick 76 69 95 94 78 64 99 89 110 88
Alex 95 88 89 78 95 69 88 101 99 89
Marga 96 67 89 76 64 94 98 83 78 56
Mike 76 89 95 64 78 34 99 89 104 98
Bella 85 68 89 78 45 69 98 101 99 89
Priya 86 77 89 76 94 94 78 89 78 96
Karen 78 69 95 94 78 94 99 89 110 88
Amit 95 88 79 78 95 89 88 101 99 89
Please see sample output on the next page. Sample Output:
Here is the grade database:
Name 1 2 3 4 5 6 7 8 9 10 Bob 56 67 83 76 84 94 68 86 78 56 John 76
89 95 64 78 34 99 89 104 98 Joe 65 68 89 78 45 69 98 101 99 89
....
....
....
....
Bob’s highest grade is: 94
Bob’s lowest grade is: 56
John’s highest grade is: 104
....
....
The average of their grades is as below: Bob 74.8
John 82.6
....
....
Please help.
Thank you.
The following code of the program is explained with comments. Please let me know if you have any doubts regarding the code.
Code:
#include <iostream>
#include <fstream>
#include <climits>
#include <iomanip>
// This constant stores the row size of the
arrays
const int CAP = 100;
// This constant stores the column size of the
arrays
const int NUM_GRADES = 10;
using namespace std;
// This function reads data from input file into the
arrays
int readData(ifstream &iFile,int scores[][10],char
names[][30])
{
// Printing the headers for the student data
cout<<"Here is the grade database:"<<endl;
cout<<"Name 1 2 3 4 5 6 7 8 9 10"<<endl;
// This variable stores how many records have been
read
int items_size = 0;
// Until EOF is reached, this loop is
iterated
while(!iFile.eof())
{
// Reading the student name into the names
array
iFile >> names[items_size];
// Printing the student name
cout<<names[items_size]<<" ";
// This loop iterates NUM_GRADES times
for(int i=0;i<NUM_GRADES;i++)
{
// Reading the marks of the quiz i of the student into the
scores array
iFile >> scores[items_size][i];
// Printing the marks
cout<<scores[items_size][i]<<" ";
}
// Printing a new line
cout<<endl;
// Incrementing the items size
++items_size;
}
// Printing a new line
cout<<endl;
// Returning number of records read
return items_size;
}
// This function calculates the lowest score of each
student
void findLowestEach(int scores[][10],char names[][30],int
size)
{
// Iterating over names and scores of
students
for(int i=0;i<size;i++)
{
// This variable stores the lowest score of the
student
int lowest_score = INT_MAX;
// Iterating over scores
for(int j=0;j<NUM_GRADES;j++)
{
// If the score at index i is greater than value at
lowest_score
if(lowest_score > scores[i][j])
{
// Then update the value of lowest_score to
scores[i][j]
lowest_score = scores[i][j];
}
}
// Printing the name of the student with their lowest grade
found
cout<<names[i]<<"'s lowest grade is:
"<<lowest_score<<endl;
}
// Printing a new line
cout<<endl;
}
// This function calculates the highest score of each
student
void findHighestEach(int scores[][10],char names[][30],int
size)
{
// Iterating over names and scores of
students
for(int i=0;i<size;i++)
{
// This variable stores the highest score of the
student
int highest_score = INT_MIN;
// Iterating over scores
for(int j=0;j<NUM_GRADES;j++)
{
// If the score at index i is lesser than value at
lowest_score
if(highest_score < scores[i][j])
{
// Then update the value of lowest_score to
scores[i][j]
highest_score = scores[i][j];
}
}
// Printing the name of the student with their highest
grade found
cout<<names[i]<<"'s highest grade is:
"<<highest_score<<endl;
}
// Printing a new line
cout<<endl;
}
// This function calculates the average grade of each
student
void calcAverage(int scores[][10],char names[][30],int size)
{
// Iterating over names and scores of
students
for(int i=0;i<size;i++)
{
// This variable stores the average grade of each
student
double average = 0;
// Iterating over scores
for(int j=0;j<NUM_GRADES;j++)
{
// Adding score of each student to average
variable
average += scores[i][j];
}
// Calculating the average of student
average /= NUM_GRADES;
// Printing the name and the average grade of the
student
cout<<names[i]<<"
"<<setprecision(3)<<average<<endl;
}
}
// This is the main function
int main()
{
// This char array stores the names of the
students
char names[CAP][30];
// This int array stores the scores of the
students
int scores[CAP][NUM_GRADES];
// This variable stores number of students data present in
the input file
int items_size;
// This variable stores the name of the input
file
string input_file_name =
"J:\\cpp_dev\\high_low_grades\\data.txt";
// Creating the ifstream object and opening the input
file
ifstream inFile(input_file_name);
// Reading the scores and names from the input file into
the arrays and returning number of records read
items_size = readData(inFile,scores,names);
// Calculating lowest score of each student
findLowestEach(scores,names,items_size);
// Calculating highest score of each student
findHighestEach(scores,names,items_size);
// Calculating average grade of each student
calcAverage(scores,names,items_size);
return 0;
}
contents of data.txt:
Bob 56 67 83 76 84 94 68 86 78 56 John 76 89 95 64 78 34 99 89 104 98 Joe 65 68 89 78 45 69 98 101 99 89 Amy 86 77 89 76 94 54 78 89 78 66 Nick 76 69 95 94 78 64 99 89 110 88 Alex 95 88 89 78 95 69 88 101 99 89 Marga 96 67 89 76 64 94 98 83 78 56 Mike 76 89 95 64 78 34 99 89 104 98 Bella 85 68 89 78 45 69 98 101 99 89 Priya 86 77 89 76 94 94 78 89 78 96 Karen 78 69 95 94 78 94 99 89 110 88 Amit 95 88 79 78 95 89 88 101 99 89
Sample Output: