Homework 4 - File I/O and Arrays

Due: Wednesday, October 29, 2008 at 5:00pm
Note: Late homework will NOT be accepted after 5:00pm on Fri. Oct 31, so that the solutions can be posted for people to study them before Midterm 2.

Name your files hw4_<problem>.cpp, such as hw4_1.cpp. Email all the cpp files to my Helios account.

  1. (5 points)Write a program that reads a text file in character-by-character and performs the following normalization tasks: You will have two file streams: an input file stream for the file to be normalized and an output file stream that contains the normalized file. You should issue an error message and exit if either file cannot be opened. Your program should prompt the user for the filename for the file to be normalized (the input file). The output filename should be the input filename with ".norm" added to it. For example, if the input file is "data.txt", the output file name will be "data.txt.norm".

    Sample input file: hw4_input1

  2. (5 points)Write a program that reads integers in from the keyboard and stores them into an array. The program will prompt the user for the number of integers they wish to enter and then use a for() loop to read that many integers into the array. Once the integers have been obtained, compute the average and standard deviation and print that to the screen. To compute the standard deviation, you first compute (si - a)2 for each integer, where a is the average and si is the value of the integer, and add the results to a sum (i.e. you're computing the summation of (si - a)2 for the whole array). You then take the sum and divide by the sample size (i.e. the number of integers in the array) minus one. Take the square root of the result to get the standard deviation.
  3. (10 points)Create the following menu program. You should use a do-while loop to print the menu and a switch() statement to parse the options.
      Welcome to the CS221 Homework 4 Menu
      ===============================================
      1.  Enter a list of integers from the keyboard.
      2.  Read a list of integers from a file.
      3.  Print the list of integers.
      4.  Print the sum of all integers in the list.
    
      0.  Exit
      ===============================================
      Enter selection:
    
    When option 1 or 2 is selected, the list of integers will be stored in an array. If there is already integers in the array, overwrite the integers with the new data. Option 2 should prompt the user for a filename and read the integers in from that file. If the file cannot be opened, print an error message but do NOT exit the program. If option 3 or 4 are selected before option 1 or 2, an error message should be printed that reminds the user to enter data via option 1 or 2.

    Sample input files: hw4_input2 hw4_input3