Lab 4
Lab assignment: Write a slightly harder 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.
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 we want you to use functions for each of the operations. There should be no mathematical operations in main besides the assignment statement, all of the operations must be performed in separate functions outside main. 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. Nor should you have any global variables.
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. As a starting point use the sample file called lab4.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):
Add one more operation to your calculator: exponentiation, that is the first number to the power of the second number. This should be menu option 5. For example consider the case where the current value stored in the calculator is 3 and the user menu selection is 5 for exponentiation. If the user then enters the number 2, then the result would be 3 to the 2nd power, which is 9. You must do this by setting up a loop to perform the correct number of multiplications. Keep in mind that you may not use global variables or change the given parameters. (You may not use the pow method, in case you know what that is.)
Stage 3 (Extra Credit) (1 point):
Add yet another operation to your calculator: inverse exponentiation, or nth root, which calculates the first number to the power of 1 divided by the second number. This should be menu option 6. An example of this is the cubed root of 64. If the first number was 64, and the number 3 was entered as the second number, then the calculator would produce the result of 64 to the 1/3 power, which is 4. (4*4 = 16, 16*4 = 64, so 4*4*4 = 64)
You should do this via Newton's method for approximations. There's a description of an algorithm I posted on Piazza, and also here for your convenience. Use a tolerance of .01 or less, that is you should keep applying the formula until the difference between g-new and g-old is less than .01. For larger roots, you will need to use smaller values. You may return an integer however, to fit with the format of the rest of the calculations. Also you'll need an initial guess. You can use any number for this, I used the current value, the value whose root we were taking, but you can choose a smaller number such as value1/value2 to converge in fewer iterations. Note that the derivative of f(x) = xn is f'(x) = n*xn-1. Since your exponentiate method only works on integers, you may use the builtin pow method from math.h for taking exponents in this function.
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.