Question

In: Computer Science

(C++) Write a program to read from a grade database (data.txt). The database (text file) has...

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Solutions

Expert Solution

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:


Related Solutions

Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT