Assignment 06

Due: Friday, March 10, 2017 at noon 100 pts

You know how to submit; do it in the usual way. However, this time you will submit multiple files for this programming project. The cssubmit script will work as usual, picking up all .cpp and .h files in the current directory. So, make sure you have created a separate directory for your hw 6 program. Do not create sub-directories in the hw6 directory.

Background: The background for hw 6 is hw 4. That's right, you are going to rewrite hw #4, but this time using functions. And, there are to be modifications. You are going to code a rude vending machine that has a pension for snarky comments and a mind of its own.

Specifications: Your program is to function pretty much the same as your solution (assuming it was correct) for hw #4. However, you're going to add some functionality as described below. Also, and most importantly, you are to "function-ize" the code as much as practicable. That is, you will create functions to do much of the work, and your main will call functions, and they will call functions....and maybe even they will call functions. The idea is to make your code easier to use and read. The same restrictions on using libraries is in place - no use of cmath, for example.

Modifications: Here is a list of the modifications and additions to the project:

  • Your main menu will have more options. For option 1, offer to compute the factorial of any input. Prompt for the input (should only allow non-negative input less than or equal to 10).

  • For option 2 (exponentiation), you will prompt for any value of x for calculating ex . You will use the formula "pattern" given in hw 4: ex = 1 + x + x2/2 + x3/6 + x4/24 + x5/120 + ... = x0/0! + x1/1! + x2/2! + x3/3! + x4/4! + x5/5! + x6/6! + ... (Note: 0! = 1) The number of terms used in this calculation is 8. But that should be changeable to any positive integer at a moment's notice! (In other words, you'd better use loop(s) for this computation.)

  • For option 3 (sin(x) ), you will do the same as in hw 4.

  • For option 4 (roots), you will not have a secondary menu. You will prompt for the value, x, to find the root of. But then you will prompt for which root to find: square root (n = 2), cube root (n = 3), fourth root (n = 4), or fifth root (n = 5). The formulas for the nth root calculations follows a pattern. Can you see the pattern? If not, it's written below. Output the designated root and return to the main menu. (If the user wants another root, they have to pick option 4 again.) Use 10 iterations for the computation.

  • For option 5 (another new option) you are to compute the hyperbolic cosine of a value, cosh(x). Most of you don't know what that is. Here's the formula: cosh(x) = (ex + e-x)/2 = x0/0! + x2/2! + x4/4! + ... + x2k/(2k)! + ... So there are two things to note here. One is that you can compute this using either the exponential expressions or the series. The second is that the series is just like the cosine series except that the terms are all positive. Interesting, no? Interesting yes! If you use the series, use 6 terms.

Roots formulae: To compute roots, use this......... the kth root of A is given by this iterative formula: xn+1 = [(k-1)*xn + A/xn(k-1)] / k . Use A as your first (initial) value for the root, x0.

Thoughts: Use functions wisely.

  • It should be obvious now that you should write functions for all of the above options, along with functions for powers and factorials to use in the functions for the various options ..... such as sine and cosh and ex.

  • Do not copy code from the posted solution for hw 4.

  • Do use the truncated Maclaurin series (the formula above) for ex and sin(x) etc.

  • You should double for a computational type. The numbers for these calculations can get big, so you will need precision on your side.

  • Don't make a do_everything function. Some people do this:

      • int main()

      • {

      • do_everything();

      • return 0;

      • }

    • This is nothing more than renaming main; it accomplishes nothing. Leave something in main....perhaps your switch.

Also: The description of the program above is .... well, boring. Let's make this fun. You are going to code a Rude Calculator. Yep, you read correctly. Code this program so that, with a probability of 0.25 (1/4 of the time), any time the user makes a menu choice, your program will NOT give them what they want but rather outputs one of the following messages chosen at random:

  1. "No. I'm not going to do that for you."

  2. "That's not something you need to know."

  3. "Can't you do that on your own? Why bother me with your problems?"

  4. "Perhaps you need to see your psychologist for this problem."

  5. "What do you think I am? Your patsy?"

  6. "I don't feel like doing that. Come back later and ask me."

  7. "Go away!!"

  8. "It's NOT my problem."

  9. "Ask Milhouse....surely he knows the answer to your stupid question."

So, how do you do this?? Here's one way: Have your code pick a number randomly from 1 to 9 and pass this value to a function that contains a switch statement that picks the message. Now, after the message is output, you should return control to the main menu. Let them try their luck with your snarky calculator!!

Next, any time the user is asked to input a value to compute the exp or sin or cosh or factorial for, your program will have a 35% chance that it will come back with, "I don't like that number. I like X better". X will be chosen at random between 3 and 9, inclusive. Your program will compute the functional value of the new number it likes.

Also Also: Don't forget to document your functions! And of course, don't use the cmath library!

When you submit: Use the following inputs. If at any point in the execution of your program during the submission process as described below your input is met with sass-itude1 from the program, continue to try that input until real results are obtained (for that choice and x-value).

  • option 1

  • x = 5

  • option 3

  • x = 1 using 8 terms

  • option 4

  • x = 60 find the fifth root

  • option 4 again for x = 90 find the 3rd root

  • option 5

  • x = 2

  • option 2

  • x = 4

  • quit.....and if you get 'lucky' by getting a snarky response to choosing quit, just try again....until it lets you.

And, as usual, ask your instructor if you have any questions.

1program attitude2

2snarky software3

3pre-teen app4

4Whatt EVVVVERRR