CIS 150 Spring 2018 Midterm exam #2 practice problem

Objectives

  • Write proper documentation comments.
  • Create a program using proper style conventions.
  • Create a program using C++ include directives and "using" statements.
  • Create C++ function prototypes.
  • Declare variables using C++.
  • Get user input.
  • Display output.
  • Display formatted output.
  • Call functions, sending the appropriate values, and storing returned values as needed.
  • Create functions to perform specified tasks.
  • Create arrays, process arrays, and send arrays to functions.
  • Perform sequential text file access.

Program requirements

  1. Write documentation comments. They must include the name of the program, the date, the course number, and your name.
  2. Use proper indentation throughout the program using four spaces per level of indent.
  3. Use spaces and NO tabs for the indentation.
  4. Write whatever include directives and using statements that your program needs.
  5. Write a function prototype for a function named loadArray that takes an array of doubles and an integer length and returns nothing.
  6. Write a function prototype for a function named writeFile that takes a string filename, an array of doubles, and an integer length, and returns a Boolean value.
  7. Write a function prototype for a function named getFileAverage that takes a string filename and returns a double.
  8. In main:
    1. Declare a string to hold a filename and initialize it to: practice2.txt
    2. Declare a constant integer variable named SIZE and set it to 5.
    3. Declare an array of doubles of length SIZE.
    4. Send the array and the length of the array to the loadArray function.
    5. Send the filename, the array, and the length of the array to the writeFile function.
    6. If the writeFile function returned false, then display an error message and return an error exit status.
    7. Send the filename to the getFileAverage function.
    8. Display the value the getFileAverage function returned using fixed point format with 2 digits of precision.
    9. Return with a success exit status.
  9. Write a function named: loadArray:
    1. The parameters to this function are an array of doubles and an integer array length.
    2. This function returns nothing.
    3. Write a loop to get doubles from the user and store them in the array.
  10. Write a function named: writeFile:
    1. The parameters to this function are a string filename, an array of doubles and an integer array length.
    2. This function returns a Boolean value.
    3. Open the file specified for sequential text writing. [NOT appending]
    4. If an error occurred during file open, then return false.
    5. Write a loop to output each element of the array to the file, with a newline after each value output.
    6. Close the file.
    7. Return true.
  11. Write a function named: getFileAverage:
    1. The parameter to this function is a string filename.
    2. This function returns a double value.
    3. Create a variable to hold a sum and another to hold a count. Initialize both to 0.
    4. Open the file specified for sequential text reading.
    5. If an error occurred during file open, then return false.
    6. Write a loop to read each double in the file. For each element read, add it to sum and increment the count.
    7. Close the file.
    8. Return the average (sum divided by count).

Sample output run

Enter number 1: -2.8 Enter number 2: 99.2 Enter number 3: 7 Enter number 4: 15.3 Enter number 5: 3.3 The average is 24.40