CIS 150 C++ Spring 2017 Midterm exam #2 practice problem
Overview
Write a program to meet the listed requirements. Note: This includes more code than will be on the actual midterm #2.
Program requirements
- Write documentation comments. They must include the name of the program, the date, the course number, and your name.
- Use proper indentation throughout the program using four spaces per level of indent.
- Use spaces and NO tabs for the indentation.
- Write whatever include directives and using statements that your program needs.
- Write a function prototype for a function named writeToFile that takes a string filename and an integer length. It returns a Boolean value.
- Write a function prototype for a function named readFromFile that takes a string filename. It returns a Boolean value.
- Write a function prototype for a function named fillArray that takes an integer array and an integer length. It returns nothing.
- Write a function prototype for a function named displayArray that takes an integer array and an integer length. It returns nothing.
- Write a function prototype for a function named sumArray that takes an integer array and an integer length. It returns an integer.
- Write a function prototype for a function named writeArrayToFile that takes an ofstream reference, an integer array, and an integer length. It returns nothing.
- In main
- Initialize the random number generator.
- Declare a string object named filename and initialize to "practice2.txt".
- Declare a constant integer name SIZE and initialize to 12.
- Declare an integer array of length SIZE.
- Output the message: Basic file I/O test\n
- Call writeToFile, sending it the filename and SIZE.
- If writeToFile returned false, display an error message (Output file error).
- If writeToFile returned true, then call readFromFile, sending it the filename.
- If readFromFile returned false, then display an error message (Input file error).
- Output the message: Array and file I/O test\n
- Call fillArray, sending it the integer array and SIZE.
- Call displayArray, sending it the integer array and SIZE.
- Calling sumArray. sending it the integer array and SIZE.
- Display the sum you got back from sumArray.
- Declare an ofstream object and use filename to open it. Open it for appending.
- If the output file failed to open, then display an error message.
- If the output file did open, call writeArrayToFile, sending it the ofstream reference, the integer array, and SIZE. Then close the ofstream (file) object.
- Call readFromFile, sending it the filename.
- If readFromFile returned false, then display an error message (Input file error).
- Return success (0).
- Write a function named writeToFile
- Parameters: string filename, integer length
- Returns: true or false
- Open file for output.
- If there is an error, then return false.
- If there is no error, then
- Output length random numbers between 1 and 100 to file, with each one followed by a newline.
- Close file.
- Return true.
- Write a function named readFromFile
- Parameters: string filename
- Returns: true or false
- Open file for input.
- If there is an error, then return false.
- If there is no error, then
- while you can read in an integer successfully, display that integer with a space after it.
- Close the file.
- Return true.
- Write a function named fillArray
- Parameters: an integer array and an integer length
- Returns: nothing
- Use a loop to set each element of the array to a random integer between 1 and 500.
- Write a function named displayArray
- Parameters: an integer array and an integer length
- Returns: nothing
- Use a loop to display each element of the array, with a space after each element.
- Output a newline after all the elements are displayed.
- Write a function named sumArray
- Parameters: an integer array and an integer length
- Returns: an integer
- Use a loop to total up all the elements of the array.
- Return the total.
- Write a function named writeArrayToFile
- Parameters: an ofstream reference, an integer array, and an integer length
- Returns: nothing
- Use a loop to output all the elements of the array to the ofstream (file), with a newline afetr each element.
Sample output run
Basic file I/O test
842 741 557 227 130 703 29 6 11 361 128 748
Array and file I/O test
495 24 382 129 154 490 95 114 231 261 392 37
The sum of the array is 2804
842 741 557 227 130 703 29 6 11 361 128 748 495 24 382 129 154 490 95 114 231 261 392 37