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..
Stage 1 (1 point):
Write a calculator which is able to perform any of the 4 basic operations, looping back to the "menu" and keeping the result of the previous operation as the first number in the next operation. It should ask once what operation to perform on the initial value of 0, and then based on that input perform that operation, print out the result, then "loop back" and ask the user what operation they would like to perform next. Each of the functions take in 3 ints, one to store the result of the operation, then the first number to operate on, and finally the second number to operate on, but return no values. As a starting point use the sample file called lab5.c provided at the bottom of this page. You may also wish to look at last week's solution as a further aid.
Stage 2 (1 point):
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. Keep in mind that you may not use global variables, or change the given parameters. You may not use the - operator, -- operator, + operator, ++ operator, / operator or the multiply operator (though you can and probably should use the * dereference operator) in the add and multiply functions for this step, but you may use these operators in the function addOne itself.
Stage 3 (Extra Credit) (1 point):
Implement the extra credit functions. These only take 2 values, as opposed to the functions for part 1, and addition, multiplication, and exponentiation require you to use only assignment, looping, branching, and the ECaddOne function, which is pass by reference, taking in a single integer value and returning nothing.
Notes:
Keep in mind that this is a team effort so you should agree with your partner on what you are going to do before you start typing. The partner who is typing is the "driver" and the partner watching is the "navigator." Be sure to switch roles every 10 to 15 minutes, to foster a deep understanding of the code for both partners. The navigator should be watching for syntax errors and verifying the correctness of the code you're writing.
It will speed things up for you if you keep a window open for editing and have a separate window open for compiling and running your program. Remember that windows are resizeable!
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.