Lab 5

Lab assignment: Write yet another Calculator

In this week's lab you must write a C (not C++) program to simulate a simple integer calculator.  You will need to know simple input/output, declaring variables, if statements, arithmetic operations, a loop of some kind, and how to write and utilize functions using both pass by value and pass by reference, possibly until you are sick of them.

The execution of the finished program should look like what is shown below.  It should be very similar to last week's, so please follow the precise format given.  The value from the previous operation should carry over into the next operation, and there shouldn't be any extra lines between operations.  (User input is shown in bold, though in your program it will not be bold.)  

Welcome to the calculator.

Your current value is 0

Options are:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  9. Exit

Please enter an operation -> 1

Please enter the number to be added -> 4

Your current value is 4

Options are:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  9. Exit

Please enter an operation -> 4

Please enter the number to be divided -> 3

Your current value is 1

Options are:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  9. Exit

Please enter an operation -> 9

Your final value is 1.  Exiting program...

The difference from last week's lab is that our primary functions, the one representing the four operations, should not return a value, instead modifying one of the parameters.  That is, you should call the functions passing in the parameters by reference.  I've given you a framework which includes all the function definitions, so fill out the definitions with code to actually perform the calculations, then make the correct calls in main to perform those calculations.  Do not modify the function headers, to rename the functions or change the parameters passed in.  Once again, no global variables.

For the second point, modify both the addition and the multiplication functions to use only the assignment statement, if statments, for statements (or while/do while statements) and the function addOne, which you will have to write.  Note that addOne is a pass by value function, not pass by reference.

For extra credit, fill out and use the bonus functions ECadd, ECsubtract, ECmultiply, and ECdivide, and also ECexponentiate, with the same conditions as point 2 for EC exponentiate..

Notes:

Submission:

1. You should work with a partner for this (and all the remaining) lab(s).  Only one of you need to submit the program to Blackboard, though you should be certain that both of your names be present in a comment at the top of the .c or .cpp source file.

2. You should turn in to Blackboard by the END OF THE LAB (8:50 for the 8-9 lab session, 9:50 for the 9-10 lab session).  I know it's tempting to keep working on it, but other classes come in, and it's not fair to the students who are limited to that particular time span if you go over.  Which isn't to say that you can't work on it later, to check your solution against the one I post for your own understanding.  But what you submit for a grade should be before the next hour begins.

3. If you wish, you may submit your lab by 11 am on Thursday for a 1 point penalty.  If you can't finish up the second point by the end of lab, you can still earn the score by completing all three steps and submitting your code by the day after.