Assignment 2: A dynamically sized array

due: 11:59 PM, Thursday, Jan. 23, 2003

This assignment has two parts: a required part and an optional (challenging) part.

The required part of your assignments is to write a program that reads and print (almost) unlimited number of integers.

Your program should give the user 3 choices:

1. Enter numbers
2. Print numbers
3. Quit

If the user chooses to enter numbers, your program should continually prompt the user for an integer. When the user enters 0 your program should return to the main menu. The integers should be stored in a dynamically allocated array. One complication is that you don't know in advance how many integers will be entered. There are many ways to deal with this problem. While you are free to choose your own approach, we recommend the following strategy:

First, you allocate dynamically a small array of integers. When the array becomes full, you double its size. The doubling can be accomplished by first allocating a new array, twice the size of the old one, copying the integers from the old array to the new one, and finally, deallocating the space for the old array.

If the user chooses to print numbers, your program should print all integers entered so far and return to the main menu.

Quit terminates your program.

Here is an example of what your program should do:

Example:

joe@prelude% a.out
Please, choose one of the following 3 options:
1. Enter numbers
2. Print numbers
3. Quit
Your choice:
1
Enter a number:
3
Enter a number:
19
Enter a number:
0
Please, choose one of the following 3 options:
1. Enter numbers
2. Print numbers
3. Quit
Your choice:
2
3
19
0
Please, choose one of the following 3 options:
1. Enter numbers
2. Print numbers
3. Quit
Your choice:
1
Enter a number:
7
Enter a number:
8
Enter a number:
0
Please, choose one of the following 3 options:
1. Enter numbers
2. Print numbers
3. Quit
Your choice:
2
3
19
0
7
8
0
Please, choose one of the following 3 options:
1. Enter numbers
2. Print numbers
3. Quit
Your choice:
3
joe@prelude%

The optional part of your program, is to add two more choices to the main menu:

     4. Enter operators
     5. Compute

The "Enter operators" option allows the user to enter any number of arithmetic operators (+, -, *, /), one at a time. If the user enters 0 the program should return to the main menu.

The "Compute" option computes with the expression obtained by interleaving the array of integers with array of operators with no operator precedence.

For example, if the array of numbers is: 1, 5, 7, 3 and the array of operators is +,*,-, then the expression is: 1 + 5 * 7 - 3. Without operator precedence this expression evaluates to ((1+5)*7)-3 = 39.

Submission Instructions

Do all the following while logged into classes.cs.uchicago.edu.

  1. Create an empty directory called hw2.
  2. Name your program dynamic.cc, and copy it into this directory.
  3. Your code should begin with a comment along the lines of
       /*********************************************
        *                                           *   
        *  I, Joe Schmoe, a.k.a. jschmoe@cs, wrote  *
        *  and tested this program myself, without  *
        *  assistance or collaboration.             *
        *                                           *  
        *********************************************/
    
    Of course, this does not preclude you from asking general questions about C++ (to anyone), or from asking the instructor or TA's for clarification of the assignment.
  4. Compile your code by typing g++ dynamic.cc at the command line.
  5. Alternatively, you may divide your source code into several source and include files, as well as a Makefile with a target all which properly compiles your program. In this case, test your submission by typing make. Make sure you are on one of the Linux machines (e.g. classes) when you do this!
  6. Do a sample run of your program, and copy the results, including the user input, into a text file called trial.txt. Choose the trial input carefully to demonstrate that your program functions correctly even on "hard" cases. The contents of this file must agree with the actual behavior of your program.
  7. Delete all files except dynamic.cc and trial.txt from this directory. (If you are submitting multiple source files and a Makefile, then only delete the compiled versions and junk.)
  8. cd ..   to change to the parent directory.
  9. Submit your assignment with the command
    hwsubmit 11600 hw2
    
    You should get a message indicating that the correct files are being copied. If you get error messages or no message, then send email to one of the TA's before the submission deadline.
  10. Remark: Although very little credit will be awarded to programs which do not compile, still less will be given to programs turned in late. Please allow enough time to do all the submission steps before the deadline. Also, note that there is no penalty for repeat submissions (up to the deadline), so if you submit early, then discover a hidden bug, you may fix it and resubmit without penalty. In this case, you should include a comment explaining the change.