Computer Science I
MC140.01
Spring 2001
Assignment 3
due Wednesday 7 February 2001

The goal of this assignment is to practise using loops (for, while, do), if/else and switch statements and flow charts.
Read through the ENTIRE assignment before you begin.

This assignment is worth 2% of your semester grade -- i.e., it will be marked out of 20 points.

For this assignment, you will write a program that emulates a simple calculator.

First, the program should ask (or "prompt") the user to enter an operator to perform either addition, subtraction, multiplication, (integer) division or modulus. We haven't learned how to input characters yet, so you will have to use numbers to indicate which operator to use. For example, if the user enters a 1, then addition should be performed. Here's what the prompt line should look like:

enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit):

Next, your program should prompt the user to enter two numbers (integers). Here's what this prompt line should look like:

enter two numbers:

Next, your program should read in the two numbers and store them in integer variables. Then, your program should perform the requested operation and output the answer.

Finally, your program should loop back to the "enter an operator" prompt, repeating these steps until the user specifies that s/he wants to quit.

Here's a sample run. The numbers in big, bold font were entered by the user. The rest was output by the computer.

enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 1
enter two numbers: 3 4
x + y = 7
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 2
enter two numbers: 8 5
x - y = 3
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 3
enter two numbers: 9 7
x * y = 63
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 4
enter two numbers: 10 4
x / y = 2
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 5
enter two numbers: 7 3
x % y = 1
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 7
invalid operator
enter an operator (1 for +, 2 for -, 3 for *, 4 for /, 5 for %, 6 to quit): 6
thanks for playing!

1. Create a flow chart for the program. I strongly suggest that you do this before you write any code. You must hand in the flow chart along with your hard copy. The drawing does not need to be done on the computer. Freehand drawing is fine, as long as it is neat and legible. (5 points)

2. Write the program, using a sentinel controlled while loop to control how many times to ask for an operator and two numbers. Use a switch statement to determine which calculation to perform, based on the operator entered. Name your program <your-user-name>-ass3.c (e.g., sklarel-ass3.c). (11 points)

3. Make sure you do the following:

• You must include a header comment at the beginning of your source code file. The header comment must include your name, the assignment number and the date.  (1 point)
• You must include comments after each closing curly bracket ("}") indicating which program block is being ended. For example:  } /* end of for loop */  (1 point)
• You should indent your program according to the conventions we have been using in class or those used in the book. This means, each program block is indented.  (1 point)
• Your program should do error checking and output an error message (like "invalid operator") when the user enters an operator that is not between 1 and 6.  (1 point)
4. Submit the assignment in the usual way (i.e., follow the submission instructions from assignment #1). Make sure that the name of the file you submit is entirely in lower-case letters and you use the proper naming format, as described in step 2, above. Also, bring a hardcopy to class and include your flow chart!