Homework 5 - File I/O and Arrays: Part 2
Due: Wednesday October 27, 2010 at 5:00pm
NOTE: No late assignments will be taken after Saturday October 30, so the
solutions can be posted to study before Midterm 2.
Name your file hw5.cpp and email it to my account.
Modify the class menu program to work on an array as follows. The menu
will print this to the screen:
Welcome to the CS221 Homework 5 Menu
===============================================
1. Enter a list of integers from the keyboard.
2. Read a list of integers from a file.
3. Multiply all integers in the list.
4. Calculate the average and standard deviation
for all the integers in the list.
0. Exit
===============================================
Enter selection:
The list read in by either option 1 or 2 should be saved in an array. Use
this saved array to do options 3 and 4. Options 3 and 4 should print out an
error if they are selected before input is given (via option 1 or 2).
Options 3 and 4 should use a calculation function (e.g. calc_product,
calc_average, calc_deviation) that takes an array and the size of the array
and returns the appropriate calculation. Recall from Homework 4 that the
standard deviation calculates the sum squared error (sse) then divides by
the size - 1, then takes the square root.
Required elements:
- The same array variable will be used for all options. Declare this array
in main's scope and pass it as an array parameter to the functions.
- If option 1 and 2 are selected one after the other, throw away the data
from the earlier option. In other words, input beginning at index 0.
- Option 2 MUST prompt the user for a filename, read the user's response into
a filename variable and then open the input stream for that particular
filename. Points will be deducted if a hard-coded filename, such as
fin.open("hw5_input1"), is used.
- If size is less than 0 when option 3 is selected, issue an error message
about needing to input the array from option 1 or 2.
- If size is less than 2 when option 4 is selected, issue an error message
saying that the standard deviation cannot be calculated for less than 2
elements.
- The array calculation functions will be pure calculation functions. They
will NOT cout the result, but will instead return the result. The calling
function is responsible for storing and printing the result.
You may opt to use additional functions to support your menu, such as input
functions for options 1 and 2 or a menu function to print the menu. These are
not required for the assignment, but should you opt to use them, they should
be syntactically and logically correct.
Use the following files as input to option 2:
hw5_input1
hw5_input2