Homework 1 - Dynamic Arrays

Due: Tuesday January 16, 2007 at 5pm

Name your file hw1.cpp and mail the file as an attachment to my Helios account. Hint: If using Pine on Helios, type just my username (mdanfor) in the To field and hit enter. If you have entered it correctly, my full name will come up. If it does not, then you have likely made a typo.

Coding Conventions

The following coding conventions should be used for all assignments.

Assignment

The purpose of this assignment is to learn how to use new and delete to create and delete arrays dynamically. This assignment will use a menu driven program. For an example of a menu driven array program using ordinary arrays, see menu_example.cpp. In brief, a menu driven program presents the user with a menu. The user selects an option and that option is executed. After the option completes, the user is presented with the menu again until they select the Exit option.

For this assignment, the user will be presented with the following menu:

            Welcome to the CS222 Homework 1 Menu
  ============================================================
  1.  Create a dynamic array of integers using keyboard input.
  2.  Create a dynamic array of integers using file input.
  3.  Sort the array.
  4.  Print the array.
  0.  Exit
  ============================================================
  Enter selection: 
Option 1 should ask the user for the size of the array and check that the size is a positive number. If the size is less than or equal to 0, you should print out an error message and either ask the user to enter the size again or return to the menu. If the size is greater than 0, allocate an array of that size, prompt the user to enter the data and read the data into the array.

Option 2 should ask the user for a filename and open the file. If the file open fails, an error message should be printed and you should then either exit the program (such as what menu_example.cpp does) or return to the menu. If the file open succeeds, you should then process the file to see how many integers it has, allocate an array of that size, close the file, open it again and read the integers into the array. You may need to call fin.clear() after closing the file the first time to be able to open it again depending on the method you use to count the number of integers in the file. If you know how to "rewind" the filestream, you can also use that method instead of closing the file and opening it again.

Option 3 should use the selection sort code given in the book on pages 419-420 (Chapter 7, Section 3) to sort the array from the smallest element to the largest element. If the user has not yet selected option 1 or option 2, option 3 should instead print an error message that there is no array to sort and direct the user to select an input option first.

Option 4 should print all the elements of the array. If the user has not yet selected option 1 or option 2, option 4 should instead print an error message and direct the user to select an input option first. If option 4 is selected before option 3, it will print the elements as they were entered.

The following files should work when given as input to option 2:

Note that input1 and input2 contain the same numbers, but one is formatted as one integer per line and the other is formatted as a list of integers seperated by spaces. The same is true for input3 and input4. Your code should be able to parse both file formats. You can copy these files over to your current directory using the command:
cp /usr/users/mdanfor/public_html/cs222-w07/hw1_input* .

Extra Credit

The following items can be done to earn extra credit. 2 points will be given for each item.