Assignment 06

Due: Tuesday, March 17, 2020, at noon, 100 points

For this assignment, you will submit multiple files containing a program written in C++. Name your files meaningful names and give them proper extensions.

Background: assignment #4 is basis for this assignment, but you're going to build upon it. Willie just isn't satisfied with its functionality. He wants more! He's seen expressions like xn and x1/n and sinh(x) and would kinda like to know what the rest of the world knows that he doesn't. I mean, c'mon, when a 5th grader is calculating the hyperbolic sine of 1.2 and you still think a 'hyperbolic' is a guy that drinks way too much, wow! But Willie doesn't want to put the work into learning about this stuff, he just wants a program that will calculate it for him.

Specifications: You are to write a program that will do much the same as assignment #4 asks for, but more. Here are the specs:

  • option 1 will be (nearly) the same: prompt for and read in from the user a positive real number, x. (Note, this is not required to be an integer.)

  • option 2 will calculate and display the value of xn, for a positive integer value of n that the user gets to specify.

  • option 3 will calculate and display the value of the nth root of x for a positive integer value of n that the user gets to specify. See details below for the mathematics behind this computation.

  • option 4 will calculate and display ex. See details below for the mathematics behind this computation.

I think you can see the benefit to writing functions on how the program reads, especially if you name your functions properly. Be balanced in how you map out your functionalizing (a word I just made up). At any level, you don't want to have too many functions or too few functions. Use good judgement. Also, you are required to use the standard set of comments for each of your functions: i) a function description; ii) a precondition; and iii) a postcondition. These should be placed just above each function prototype. It is NOT necessary to put them with the function definitions.

There are many ways for you to create functions for this assignment. Since this is your first program using functions, we're going to lay out some requirements, and leave some decisions up to you. You will have

  1. a function that displays a greeting. (In future assignments, you really don't need to make a function to do this.)

  2. a function that displays the menu and returns the user's choice (not necessarily a valid choice).

  3. function(s) that return valid input from the user.

  4. a function that calculates and returns xn for real x and positive integer n.

  5. a function that calculates and returns x1/n for positive, real x and positive, integer n.

  6. a function that calculates and returns ex for real x.

  7. a function that displays a sign-off or goodbye message. (same comment as above for the greeting function.)

Now, there may be other function(s) that you will want to write and include. In fact, I can think of at least one other that I would definitely write. Can you think of it? You make these decisions.

Details: Math is cool1. Here's some you really should know:

  • The mth root: can be calculated using this iterative formulation.

      • x0 = A, (m-1)xn + A xn+1 = xnm-1 for n= 0, 1, 2, 3, . . . m

    • You should be familiar with the use of a formula like this. If you replace m with 3, then you get exactly the formula in program #4. And, for example, if you want to calculate the 5th root of A, then let m = 5 in the above formula and iterate. The sequence of approximations it generates will converge to the 5th root. Now, since you want to be reasonably sure the answer is good, include 20 iterations for this computation. As m gets large, you might experience a degradation in the quality of your answer, but you'll learn the reason when you take an introductory course in numerical methods. I'd suggest you use double precision.

    • The exponential function, ex:

      • ex = 1 + x1/1! + x2/2! + x3/3! + x4/4! + x5/5! + ...

    • You will learn this in calculus II. If you were never taught, n! = 1*2*...*n, with 0! = 1 as a special case. Now, it is a foregone conclusion that you cannot add terms to this expression forever. But, if you add 11 terms (you know, up to and including x10/10!), then you'll get a pretty good approximation for ex for small values of x. That'll be good enough for Willie since he can't count past 14 anyway.

Optional:

  • Add another choice to the menu. It will calculate (in an efficient way) and display the hyperbolic sine (sinh) of x.

  • Add another choice that will compute the value of a polynomial at the current value of x. It will necessarily read in from the user the degree of and coefficients for the polynomial.

As always, if you have questions, don't hesitate to ask your instructor or the LEAD guy (in the lab Mon through Wed nights).

1Yeah, I know. But, hey, I think cows are cool.