For the assignment, declare three arrays, each of which will hold a maximum of 4
ID: 3545473 • Letter: F
Question
For the assignment, declare three arrays, each of which will hold a maximum of 400 elements:
The three arrays will be parallel arrays. This means that the information for one level can be found in the same "spot" in each array. This also means that any time information is moved in one array the same move must be made to the other arrays in order to maintain data integrity.
It is recommended that this program be written in two versions. The first version of the program will read the data set of information from standard input (the keyboard). The second version of the code will alter the first version so that the program reads from an input file, rather than standard input.
As mentioned above, this version of the program will read the data set of information from standard input. A level number of -999 will signal the end data.
Declare the three arrays of integers and an integer variable to hold the number of levels that have been completed. If any other variables are needed, those should also be declared.
Fill the three arrays by calling the buildArrays() function and put a value into the number of levels completed variable.
Display the three arrays with the title "Candy Crush UNSORTED Report" by calling the printArrays()function.
Sort the three arrays by calling the sortArrays() function.
Display the three sorted arrays with the title "Candy Crush SORTED Report" by calling the printArrays()function.
This function will display the information for the levels that have been completed. For each level, display the level number, the score for the level, and however many stars were earned for the level.
It takes five 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 level numbers, the third argument is an array of integers that holds the scores for each level, the fourth argument is an array of integers that holds the number of stars for each level, and the fifth argument is an integer that holds the number of valid level that were placed in the arrays.
It returns nothing.
This function should be coded with a loop that executes numberOfLevels number of times. Within the loop, display the level number and score directly from the second and third arguments, and then call the printStars function to display the stars that were earned for the level. The printStars function should be passed the number of stars from the fourth argument.
Use the following as a basic format for the output:
This function will display stars (asterisks) on the screen.
It takes one integer as its argument: the number of stars to display on the screen; and returns nothing.
This function should be coded with a loop that executes numberOfStars number of times. Within the loop, display a single star (asterisk).
This function will use the selection sort algorithm that was presented in lecture to sort the arrays in ASCENDINGorder based on the scores for each level.
This function takes four arguments: the first argument is an array of integers that holds the level numbers, the second argument is an array of integers that holds the scores for each level, the third argument is an array of integers that holds the number of stars for each level, and the fourth argument is an integer that holds the number of valid level that were placed in the arrays.
Each array should be able to hold 400 elements. Use a symbolic constant to represent the maximum size of an array.
Each array has the capability to hold 400 elements, however, that does not mean that they will all be used. This is the reason that the number of elements in the array is being passed to the sort and print functions. This value is the return value from buildArrays.
The majority of the changes for this version of the program will be made in the buildArrays function. The one change that is not in buildArrays is to add #include <fstream> to the top of the code. This #include is needed so that files can be used in the program.
The input for this version of the program will be read from a file named candycrush.txt. The file consists of a set of records for each level that has been completed in the Candy Crush Saga game. Each record represents one level and is made up of three integer values that are all contained on one line of the file: the first is the level number, the second is the score that was earned for the level, and the third is the number of stars that were earned for the level. The file resembles the following:
Add a declaration for an input file stream to the variable declarations within buildArrays. For example:
Before getting the first integer from the user, open the input file and make sure that it opened correctly
Note: if an error message is caused by the exit(-1) statement, add #include <cstdlib> to the top of the code
Change ALL of the statements that deal with the user input so that they read from a file rather than standard input. To do this, substitute the name of the input file stream (inFile in the example above) in place of cin. The cout statements that display a prompt for the user can also be removed since they are not needed with the data being read from a file.
If version 1 of the program had something like:
In version 2 of the program, it should now be:
The input file does not have a level number of -999 to indicate the end of data. Instead, the loop should test to see if there are records in the file. To do this, simply use the name of the input file stream as a boolean variable. For example:
As long as there are records in the file, the input file stream will resolve to true. Once all of the records have been read from the file, the input file stream will resolve to false.
Add a line of code to close the input file once all of the data has been read from the file. This means that it should be placed after the closing curly brace for the loop but before the return statement for the function. To do this, execute the following:
Add #include <fstream> (and possibly #include <cstdlib>) at the top of the program.
Copy the input file and write the program so that it reads the data from the current directory (ie. don't specify a file path). Make sure to put the data file in the same directory as your source code (.cpp) file.
I do not fully understand the point of each version. I get the first is the basic program and the 2nd is reading the information from the file given but im having a lot of trouble writing it this way. Please do not post a link to "postbin" or whatever it is. Thank you for all the help.
Explanation / Answer
Version 2