CSCI 240 | Spring 2025 |
For this assignment, write a program that will calculate the quiz percentage for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array.
The cpp file that is submitted for grading must be named assign7.cpp.
It is recommended that this program be written in two versions. The first version of the program will read a set of quiz scores from standard input (the keyboard), as has been done since the start of the semester. The second version of the program will alter the first version so that the program reads from an input file rather than standard input. The second version is the only one that will be handed in for grading.
DO NOT submit this version to the autograder.
As mentioned above, this version of the program will read the set of quiz scores from standard input. It should continue until a quiz score of -1 is entered to signal the end data and no more than 12 quiz scores have been entered.
NOTE: all the functions mentioned in this logic are described below.
Declare an array of 12 integers and an integer variable to hold the number of quizzes that have been completed. If any other variables are needed, those should also be declared.
Fill the array by calling the buildQuizArray() function. The buildQuizArray() function returns the number of quiz scores that it read. This returned value should be saved in the integer variable that holds the number of quizzes that have been completed.
Call the calcQuizPercentage() function to calculate the student's quiz percentage. The calculated quiz percentage that is returned from the calcQuizPercentage() function should be displayed with 2 digits after the decimal point following the format:
Your quiz percentage is [percent]%
where [percent] should be replaced by the calculated quiz percentage. There should be a single newline character at the beginning and end of the line.
Finally, display the student's quiz scores by calling the printQuizArray() function. Use the title "Quiz Scores".
Write and use the following 5 functions in the program.
This function fills an array with quiz scores.
It takes as its argument an array of integers that will hold the quiz scores. It returns an integer that represents the number of quiz scores that were read. Note about the return value: this value is important because it's possible that the student has not completed the semester and therefore has not taken all 12 quizzes yet.
The function should start by declaring any variables that are needed. At a minimum, there should be two integers: a subscript and another to hold the quiz score that is entered by the user.
Initialize the subscript to the beginning of the array.
Get the first quiz score from the user. Make sure to indicate that a quiz score of -1 will end the input.
In a loop that executes as long as the quiz score is not -1 and no more than 12 quiz scores have been entered, put the quiz score into the array at the subscript position, increment the subscript to the next spot in the array, and get the next quiz score from the user.
Finally, return the number of quizzes that were placed in the array (think about the subscript).
This function displays the information for the quizzes that have been completed.
It takes three arguments: the first argument is a string that holds the title for the report that is being displayed, the second argument is an array of integers that holds the quiz scores, and the third argument is an integer that holds the number of quiz scores in the array.
It returns nothing.
The function should first display the string argument followed by a line of 16 dashes on the next line. Make sure each line starts with a single newline character. The line of dashes should end with a single newline character.
In a loop that executes numberOfQuizzes number of times, display the quiz number and quiz score out of 10 points.
Use the following as the format for displaying an individual quiz score:
Quiz [quiz number]: [quiz score]/10
where [quiz number] should be replaced by the number of the quiz being displayed and [quiz score] should be replaced by the score of the quiz. The quiz number should be displayed in a field 3 spaces wide while the quiz score should be displayed in a field 5 spaces wide. Both values should be right-justified.
This function will calculate a student's quiz percentage.
It takes two arguments: the first argument is an array of integers that holds the quiz scores, and the second argument is an integer that holds the number of quiz scores in the array.
It returns a double: the calculated quiz percentage.
The function should start by calculating the sum of the quiz scores in the array.
If the student has taken more than two quizzes, the quiz percentage is calculated as follows (NOTE: the formula is long. Make sure you're getting the entire formula):
(sum of quiz scores - sum of 2 lowest quiz scores) / (10 * (number of quizzes - 2)) * 100
Notice that the sum of the two lowest quiz scores is part of the formula. This sum can easily be found by sorting the quiz scores in ascending order and then adding up the values at positions [0] and [1] of the array. However, avoid changing the array argument that is passed to the function because the order that the quiz scores were entered by the user must be maintained. Instead, create an array of twelve integers and call the copyArray() function that is described below to copy the original values from the array argument into the new array. Call the sortArray() function to sort the new array and then calculate the sum of the two lowest quiz scores using the new array.
If the student has taken two quizzes or fewer, the quiz percentage is calculated as follows:
sum of quiz scores / (10 * number of quizzes) * 100
Once the quiz percentage has been calculated, it should be returned.
This function will use the selection sort algorithm that was presented in lecture to sort the array in ASCENDING order.
It takes two arguments: the first argument is an array of integers, and the second argument is an integer that holds the number of values in the array.
It returns nothing.
This function copies a specific number of values from one array into another array.
It takes three arguments: the first argument is an array of integers that values will be copied into, the second argument is an array of integers that values will be copied from, and the third argument is an integer that holds the number of values to be copied.
It returns nothing.
The array should be able to hold 12 elements. Use a symbolic constant to represent the maximum size of the array.
The array has the capability to hold 12 elements, however, that does not mean that it will all be used. This is the reason that the number of elements in the array is being passed to the printQuizArray, calcQuizPercentage, sortArray, and copyArray functions. This value is the return value from buildQuizArray.
Enter your score for quiz 1 (-1 to quit): 10 Enter your score for quiz 2 (-1 to quit): 0 Enter your score for quiz 3 (-1 to quit): 8 Enter your score for quiz 4 (-1 to quit): 6 Enter your score for quiz 5 (-1 to quit): 9 Enter your score for quiz 6 (-1 to quit): -1 Your quiz average is 90.00% Quiz Scores ---------------- Quiz 1: 10/10 Quiz 2: 0/10 Quiz 3: 8/10 Quiz 4: 6/10 Quiz 5: 9/10
Enter your score for quiz 1 (-1 to quit): 6 Enter your score for quiz 2 (-1 to quit): 7 Enter your score for quiz 3 (-1 to quit): -1 Your quiz average is 65.00% Quiz Scores ---------------- Quiz 1: 6/10 Quiz 2: 7/10
Enter your score for quiz 1 (-1 to quit): 10 Enter your score for quiz 2 (-1 to quit): 10 Enter your score for quiz 3 (-1 to quit): 0 Enter your score for quiz 4 (-1 to quit): 6 Enter your score for quiz 5 (-1 to quit): 7 Enter your score for quiz 6 (-1 to quit): 8 Enter your score for quiz 7 (-1 to quit): 6 Enter your score for quiz 8 (-1 to quit): 0 Enter your score for quiz 9 (-1 to quit): 10 Enter your score for quiz 10 (-1 to quit): 8 Enter your score for quiz 11 (-1 to quit): 10 Enter your score for quiz 12 (-1 to quit): 8 Enter your score for quiz 13 (-1 to quit): -1 Your quiz average is 83.00% Quiz Scores ---------------- Quiz 1: 10/10 Quiz 2: 10/10 Quiz 3: 0/10 Quiz 4: 6/10 Quiz 5: 7/10 Quiz 6: 8/10 Quiz 7: 6/10 Quiz 8: 0/10 Quiz 9: 10/10 Quiz 10: 8/10 Quiz 11: 10/10 Quiz 12: 8/10
This is the version of the program that will be submitted for grading on Blackboard and on the autograder. It does the same thing as Version 1 of the program, but the data will be read from an input file rather than standard input.
The majority of the changes for this version of the program will be made in the buildQuizArray function. The three changes that are not in buildQuizArray are to:
add #include statements for the fstream and cstdlib libraries to the top of the code. The libraries are needed so that files can be used in the program.
add code to int main() to get the name of the input file from the user. This value is a string. Use the prompt "What is the name of the file? "
change the calling statement for the buildQuizArray function to include a second argument, the name of the input file
The input for this version of the program will be read from a text file. The file consists of a set of records that contain the quiz scores that a student earned on quizzes in CSCI 240. Each record represents one quiz. The file resembles the following:
10 0 8 6 9
There are multiple versions available for download:
Change the prototype and header for the function to include a second argument: a string that holds the name of the input file
Add a declaration for an input file stream to the variable declarations within buildQuizArray. Check the Using an Input File in a CPP Program for further details.
Open the input file and make sure that it opened correctly.
Use the input file stream to read a value from the file.
The input file does not have a quiz score -1 to indicate the end of data. Instead, the loop should test to see if there is data in the file.
Finally, once all the data has been read from the file, the file should be closed. This should be placed after the closing curly brace for the loop but before the return statement for the function.
The output that is produced with the quizscores.txt input file:
What is the name of the file? quizscores.txt Your quiz percentage is 87.00% Quiz Scores ---------------- Quiz 1: 9/10 Quiz 2: 10/10 Quiz 3: 10/10 Quiz 4: 9/10 Quiz 5: 0/10 Quiz 6: 4/10 Quiz 7: 10/10 Quiz 8: 8/10 Quiz 9: 10/10 Quiz 10: 0/10 Quiz 11: 7/10 Quiz 12: 10/10
Add #include <fstream> and #include <cstdlib> at the top of the program.
As with program 6, each of the functions MUST have a documentation box that describes the function. This is the last reminder about function documentation that will appear in the assignment write-ups.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
The data for this program will be read from an input file rather than using standard input (cin) or the random number generator. The file should be downloaded and saved. As mentioned earlier, there are multiple versions available for download from Blackboard, the autograder, and the course website. They are:
Each file consists of a set of records that contain the quiz scores that a student earned on quizzes in CSCI 240. Each record represents one quiz. The files resemble the following:
10 0 8 6 9
To start working with an input file, a couple of extra include statements are needed for the fstream and cstdlib libraries.
When a user entered the values for input, the C++ code used cin and the input operator (>>) to get the information from the keyboard and save it in a variable. cin is an input stream. To get information from a file, an input file stream is needed.
ifstream infile; //input file stream variable this will be used instead of cin
The input file stream variable (infile in the example) will be used in place of cin when information is needed.
Now that the input file stream variable has been created, it needs to be "connected" with the text file that holds the information to be read. The "connection" is created by opening the file:
infile.open( "quizscores.txt" ); //open the file for reading
OR
infile.open( filename ); //open the file for reading -- assuming the name //of the file to open is in a string variable //named filename
For this assignment, use version 2 with the string variable.
The open statement will try to open the file specified ("quizscores.txt" or whatever file name is saved in the filename variable).
Windows Users: the input file MUST be located in the same directory as the CPP file.
Mac Users: there are three options available to handle the input file.
Option 1: put in the set of double quotes ("") between the parenthesis for the open command, find where the file has been saved on the Mac, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on the Mac, between the quotes. If this option is used, before handing in the CPP file, remove the path name from the file name so that it only has the file name between the double quotes.
Option 2 for newer versions of XCode (12+): follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode13.pdf
Option 3 for older versions of XCode: follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode.pdf
onlinegdb Users: the input file must be uploaded to the online
compiler. To do so, click the cloud/up arrow button ( )
and select the input file. It should open in a tab next to the CPP file in the
online compiler.
Once the file has been opened, it's important to make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly, use something similar to:
if( infile.fail() ) //if the input file failed to open { cout << endl << "input file did not open" << endl; exit(-1); //stop execution of the program immediately }
In the previous programs, the values have been read by executing a cout/cin combination. Since this program is reading input from a file, substitute the name of the input file stream (infile in the example) in place of cin. The cout statement is not needed with the data being read from a file.
So, if a program had something like:
cout << "Quiz Score? "; cin >> value;
It should now be:
infile >> value;
When writing a read loop using data from a file, the loop should test to see if there is data in the file. One way to do this is to use the input file stream variable as a boolean variable:
while( infile )
As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is ONLY successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern that uses a priming and secondary read should be followed.
Finally, once all the data has been read from the file, the file should be closed. To do this, execute the following:
infile.close();