Question
Your program will need to have two arrays. One will be of type int, called studentId in this document. The other is of type double,
Your program will need to have two arrays. One will be of type int, called studentId in this document. The other is of type double, called grades in this document. You may use different names for your variables, but they must be of types int and double. If you use different names you will have to change your main function.
The maximum number of elements supported by the arrays is 20 elements.
Your program will be using partially filled arrays. You will be reading in the array values from a file. The name of the file is read in at execution time. These files will have zero or more student id and zero or more grades. You program must be able to process up to 20 student ids and grades. If there are more than 20 student ids and grades your program must only process the first 20. Do not go out of bounds on your arrays.
The format of the file is as follows (this is a subset of file grades1.txt):
1 90
2 100
3 89
4. 72
5 68
1 92
.....
So 1, above, is the student id and 90 is a grade associated with that student id. Similarly, 2 is a student id with a grade of 100, and 3 is a student id with a grade of 89, and so on.
There may be multiple entries for every student id. They will not be in order. Notice that student one is in the above list twice, but there are other student grades in between the two student 1 entries.
To read into two arrays you will need to create in ifstream object and open it for file grades.txt. Assuming you have opened an ifstream called inputFile you can read into the two arrays as follows (assuming index has been initialized and is the next student id or grade to be read in):
inputFile >> studentId[index] >> grades[index]
Make sure you check for end of file and also make sure you check to make sure there is still room in the arrays. The code above (inputFile >> ...) will return false if there is no addition data in the input file. The maximum number of entries is 20. If you have more than 20 grades in the input file your program must only read in the first 20. You will create a function to read in the file. See the list of functions below in this document.
Once you have read in the array values you will display the number of array elements you read in.
For part 2 you will display information for all of the students. You need to do this in reverse order (so the last one you read in will be output first and the first one you read in will be output last). You will create a function to display the contents of your arrays. See the list of functions below in this document.
Next you need to find the minimum grade, the maximum grade and you need to calculate the average grade for all of the grades (regardless of what their student ids are). You will be writing three functions for these three actions. The required functions are documented below. The function for calculating the minimum grade is required for part 1. The maximum grade and average of all grades are for part 2.
Finally for all student ids from 1 to 6 you need to display the grades associated with that student id and calculate the average grade for that students grades. The student ids are in the studentId array and the corresponding grade is the entry with the same index, in the grades array. There may be multiple elements in the studentId array with the same student id. They will not be consecutive elements in the array (so you need to go through all of the valid entries in the partially filled studentId array to make sure you have all of the records for that student id). You also have to handle the case where the student id does not exist in the studentId array. You should not calculate an average in this case, but you must output a message saying there are no grades for that student. This is required for part 1.
NOTE: The following functions are MANDATORY for this assignment. All required data must be passed to or returned from the functions. You are not allowed to use any global variables. Some functions are for part 1 and some are added with part 2.
Function main (Parts 1 and 2, and partially provided to you)
This is your driver function. We have included this as part of the code we give you. You will need to add the declarations for the arrays. You will also need to write the remaining functions. Function readFile (Parts 1 & 2)
This function returns an int. The return value will be the actual number of grades read in from the file (using the passed in file name). This value will be from 1 to 20 for a valid input file. If the file is not found (the open failed) or no records are found in the file you will return 0 back to the calling function.
The input parameters to the readFile function are the input file name, the studentId array, the grades array and the maximum number of elements in the array. You will open the file, read in all of the data (studentId and grade information) and close the file from this function. You will return back the return back the number of student and grade records you have stored in the array (from 1 to 20). You must be able to support any number of input records from 1 to 20. If you read in the 20th entry you can just stop reading and close the file. If you get an end of file you also need to close the file. If the file is empty or not found you return back 0. You can, optionally, display an error message if the file cannot be opened.
Here is the prototype for the readFile function: int readFile(const string &fileName, int studentId[], double grades[], int maxGrades);
Function displayReverse (Part 2)
This function returns back void. It takes three input parameters. The first is the studentId array passed as a const array. The second is the grades array, also passed as a const array. The third and final parameter is the number of entries in each of the arrays. This was returned from the readFile function.
Your function should display the array elements in reverse order, starting with the last valid entry in the arrays and ending up with the 0th elements being printed last. You should be able to do this with a for loop that starts at the last element of the array and ends up with the 0th entry. You will then decrement the index each time through the loop.
Function minimumGrade (Parts 1 & 2)
This function returns back a double value. This will be the smallest grade found in the grades array.
The first parameter is the grades array (passed as a const array) and the second parameter is the number of valid elements in the array (recall that you are using a partially filled array and there may be fewer grades read in from the input file that the maximum number supported).
The prototype for the minimumGrade function is: double minimumGrade(const double grades[], int numberGrades); Function maximumGrade (Part 2) This function returns back a double value. This will be the largest grade found in the grades array.
The first parameter is the grades array (passed as a const array) and the second parameter is the number of valid elements in the array (recall that you are using a partially filled array and there may be fewer grades read in from the input file that the maximum number supported).
The prototype will be similar to the one you have for the minimumGrade function. Function averageGrade (Part 2)
This function returns back a double value. This will be the average of all of the grades found in the grades array.
The first parameter is the grades array (passed as a const array) and the second parameter is the number of valid elements in the array (recall that you are using a partially filled array and there may be fewer grades read in from the input file that the maximum number supported).
The prototype will be similar to the one you have for the minimumGrade function. Function displayForStudent (Parts 1 & 2) The last and final function required returns a void. It takes 4 input parameters, as follows:
The first parameter is the student id you will be looking for in the studentId array. The second parameter is the studentId array passed as a const array. The third parameter is the grades array, also passed as a const array. Finally, you pass in the number of elements used in the array.
Your function will then search through all of the elements in the studentId array looking for any values in the studentId array that match the student id passed in as the first parameters.
If the student id matches the studentId array element you need to add the corresponding grade to an accumulator. You need to count this as one of the grades for this student and you need to output the grade to cout. After you have processed all of the elements for this student id you need to calculate the average grade for that student and output that. Only output the average grade if the student id was found in the studentId array.
The prototype for this function is:
void displayForStudent(int id, const int studentId[], const double grades[], int numberGrades);
Here is the sample output. Any input is in italics/bold. The output in bold is from the functions in part 2. The first set of bold output (Student id and Grade) is from the displayReverse function. The next two bold output lines are from displayMaximum and displayAverage.
Enter the file name to be read or hit enter to exit
grades1.txt[Enter]
There are 15 grade records Student id Grade 2 76.00 2 86.50 1 92.00 1 87.00 1 97.00 4 82.50 4 77.00 3 85.50 2 98.00 1 92.00 5 68.00 4 72.00 3 89.00 2 100.00 1 90.00 Minimum grade is 68.00
Maximum grade is 100.00 Average grade is 86.17
Grades for student 1 90.00 92.00 97.00
87.00 92.00 Average for student 1 is 91.60
Grades for student 2 100.00 98.00 86.50
76.00 Average for student 2 is 90.12
Grades for student 3 89.00 85.50 Average for student 3 is 87.25
Grades for student 4 72.00 77.00 82.50
Average for student 4 is 77.17
Grades for student 5 68.00 Average for student 5 is 68.00
Grades for student 6 There are no grades for this student Enter the file name to be read or hit enter to exit grades2.txt[Enter] There are no grade records Enter the file name to be read or hit enter to exit notavalidfilename.txt[Enter] File "notavalidfilename.txt" could not be opened There are no grade records Enter the file name to be read or hit enter to exit [Enter]
Here is the main function It is partially filled in, you will need to update it with the appropriate declarations:
#include#include #include #include
using namespace std;
// Function prototypes int readFile(const string &fileName, int studentId[],
double grades[], int maxGrades); double minimumGrade(const double grades[], int numberGrades);
void displayForStudent(int id, const int studentId[], const double grades[], int numberGrades);
// additional function prototypes go here
int main() {
// create the arrays. Maximum number of grades is 20
// this will contain the actual number of grades read in // from the input file int numberGrades; // input file name
string inputFileName;
cout << "Enter the file name to be read or hit enter to exit" << endl;
getline(cin, inputFileName);
while (inputFileName != "") {
// go read the file and fill in the student id // and grades array. // the return value is the actual number read in. numberGrades = readFile(inputFileName, studentId,
grades, MAX_GRADES);
if (numberGrades == 0) {
// output a message and return with a value of 4. // there were no grade records cout << "There are no grade records ";
} else {
// display the number of grade records read in cout << "There are " << numberGrades
<< " grade records ";
// display the contents of the arrays in // reverse order
} }
// Part 2 comment out the next statement // for part 1. displayReverse(studentId, grades, numberGrades);
// output numbers in the format: xx.xx cout << fixed << setprecision(2); // calculate and display the minimum grade cout << "Minimum grade is " <<
minimumGrade(grades, numberGrades) << endl; // calculate and display the maximum grade // Part 2 comment out the next statement // for part 1. cout << "Maximum grade is " <<
maximumGrade(grades, numberGrades) << endl;
// calculate and display the average grade
// Part 2 comment out the next statement // for part 1. cout << "Average grade is " <<
averageGrade(grades, numberGrades) << endl;
// for student ids 1 through 6 display the // grades and average for that student for (int id=1; id<=6; id++) {
cout << endl; // call displayForStudent to display grades // and average for this student displayForStudent(id, studentId,
grades, numberGrades);
cout << "Enter the file name to be read or hit enter to exit" << endl;
getline(cin, inputFileName); }
// return to operating system
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started