Assignment 04

Due: Thursday, Sept. 22, 2011, 12:00 noon, 100 points

For this assignment, you will submit a single C++ compilable file containing a program written in C++. Remember, to electronically submit a file to for this course, first change to the directory in which the file resides and then type in, at the UNIX prompt, the command:

cssubmit 53 section_letter assignment_number

Be sure that only the file you want to submit is in that directory - make a directory for every assignment! Remember that the submit system will deliver every .cpp file in the current directory to me, so you should only have the file for hw 4 in that directory. 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 to make sure that it will work for the submit script.

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. Square the number 3. Cube root of the number 4. Quit

Of course, if the user chooses option 2 or 3 before entering a number, your program should tell them so. 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 square of the number currently "entered" is to be calculated and displayed. When option 3 is chosen, the cube root 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, 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 do not allow the user to enter a non-positive value. Also, you will find it a lot easier to use the switch-case statement in this program, but we will accept the use of if-else's 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.