Assignment 04

Due: Monday, Feb. 24, 2020 at 12:00 noon, 100 points

For this assignment, you will submit a single C++ compilable file containing a program written in C++. Name your file a meaningful name and give it a .cpp extension since you will be compiling it. Also, make sure that you compile and run your program using the GNU (g++) compiler before submitting.

Background: Groundskeeper Willie was working in the playground of the school one day trying to dig a hole in which to plant a tree whose roots were wrapped up in a cube with burlap. So, he needed to calculate a cube root in order to dig the hole. (If this sounds really ridiculous, you're right.) Willie also found worksheets on the floor of the 6th grade classroom showing the calculations of squared numbers. Not wanting to be at a disadvantage, he wants to be able to compute such values. So, Willie would like you to write a menu-driven C++ program that will help him do these things.

Specifications: Your program should present a menu of choices to the user. It should look (something) like this:

menu ---- 1. Enter a number 2. Power the number 3. Cube root of the number 4. Quit

Of course, if the user chooses option #2 or #3 before entering a number (meaning, they have never chosen option #1), your program should tell them that is not a valid choice, then present the menu again. The menu is to be proffered at the conclusion of each operation until quit is chosen. When option #1 is chosen, the user is to be prompted for a positive integer. When option #2 is chosen, the user is to be prompted for a positive integer to which the current "entered" number (that value entered using option #1) will be raised to. That value will be calculated and displayed for the user. (So, if 5 were entered at option #1, and 3 were entered at option #2, then option #2 would conclude by displaying 53. When option #3 is chosen, the cube root of the value "entered" (the one entered by option #1) is calculated and displayed. When option #4 is chosen, the program "signs off" and terminates.

Important Rule: You are NOT to use the cmath library for mathematical computations. You are to code up these operations yourself. Below is an explanation of how you will compute a cube root.

Cool Math: In order to calculate a cube root of a number, say A, you must use the following iterative formula:

x0 = A, 2xn + A xn+1 = xn2 for n = 0, 1, 2, 3, ... 3

What this means is this: if you want to calculate A1/3 (the cube root of A), you will plug in x0 = A for xn, thus calculating x1. You will then plug that in for xn and thus calculate x2. Plugging that in for xn, you will compute x3. And so on. Stopping at the 10th iteration of this process, you will have a pretty good estimate of the cube root of A. Try doing this by hand with, say, A = 27. Using the iterative formula above, you would have

A = 27 x0 = 27 x1 = (2*27 + 27/(27*27))/3 = 18.01 x2 = (2*18.01 + 27/(18.01*18.01))/3 = 12.035 .....and so on

As usual, make your output user-friendly. Write your code as efficiently as possible: meaning it should not do anything unnecessarily. Be sure to use constants where appropriate.

Note: It is expected that you "cleanse" the input in the sense that you will immediately re-prompt any value that does not adhere to your request. In any such case, your code should issue a "invalid input..." message. Also, you will find it a lot easier to use the switch-case statement in this program and we require that usage for handling the menu.

Also, some of you might worry about value overflow because you are calculating potentially BIG numbers with option #2. Don't worry about that. Just don't test it with big numbers! We promise not to also.

Remember: When writing your code, be sure to:

  • Declare your variables with the APPROPRIATE type.

  • Use meaningful variable names.

  • Use proper spacing for indentations.

  • Use constant declarations where appropriate.

  • Include the comment block at the head of your file.

  • Comment code that needs it.

  • Be literate in your welcoming/signing-off messages and prompts and output.

When you submit this, and all subsequent programs for this class, cssubmit will compile and run (assuming it compiles) your program during the submission process. Thus, when you submit, you will have to enter inputs as a user of the program. Now, in order to make the output uniform for the grader and to keep her/him sane, ALL OF YOU will enter the same information. For this assignment, it is:

  • choose option 2

  • choose option 1 and enter 88

  • choose option 2

  • choose option 3

  • choose option 1 and enter -6

  • (choose option 1 and) enter 6

  • choose option 3

  • choose option 4

Optional: If you have the time and/or you're bored with life and want something else to do, try this. Write your code so that the number of iterations for computing the cube root is not "hard coded". You can either enter the tolerance for correctness of answer from the user, or set up a constant for that. You could also read in from the user an initial guess at the root.

As always, if you have questions, don't hesitate to ask your instructor or the LEAD tutor.