Homework 5 Solution


Category: Tag:




Create a subdirectory named “hw5” in your cs410 directory. Use that subdirectory for your all file submissions on this assignment. At the end of the homework assignment, as a minimum, these files should be found in your hw5 directory:

Calculator.cpp (function definitions) Calculator.h (function declarations)

hw5.cpp (main containing the switch cases for the main menu)

a “typescript” file demonstating program compilation, execution and testing

You may include other source/header files if you please.

Background: Our friend Hans Moleman has been through a series of health problems these past weeks. Interestingly, he believes he has paid more on his bills than he should have. He feels very vulnerable with his finances because he is very bad at math. Now he wants to improve his math skills, so you are going to write a program that will help him check his work. He will be manually computing values for exponentials, trig functions, and roots of numbers. So he needs a “calculator” of sorts so he can check his answers.

Specifications: Your program will begin by presenting a menu with these choices:



  1. Factorial of x

  2. Exponential of x

  3. Cosine of x

  4. Roots of x

  5. Hyperbolic sine of x

  6. Quit

In fact, your program should present this menu until the user chooses the quit option.

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. Do not use the cmath library.

When option #1 is chosen, offer to compute the factorial of any input. Prompt for the input (should only allow non-negative input less than or equal to 10)

When option #2 is chosen, 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.)

When option #3 is chosen, the user is to be prompted for the value of x (any real value) for which to compute the cos(x). But then, they are to next be prompted for a positive integer between and including 1 and 5 representing the accuracy of the computation. This value will determine the number of terms to be included in the truncated Taylor series which calculates an approximation to the cos(x). This is that formula:

cos(x) = 1 – x2/2! + x4/4! – x6/6! + x8/8! … + x2k/(2k)!

k = 1, 2, 3, …

Thus, if one wants 2 terms, that corresponds to k = 2 in this formula and cos(x) = 1 – x2/2!

Of course, it’s not really equal (=), but we’ll pretend. By the way, the larger k is, the better the approximation is. And if you add on terms forever, the equality is indeed guaranteed. Try it! It’s definitely worth the time.

Note: k! is “k factorial” is k * (k-1) * (k-2) * … * 3 * 2 * 1 (e.g. 4! = 4*3*2 *1 = 24). And, of course, output the value.

When option #4 is chosen, 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 is written below.

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.

Output the designated root and return to the main menu. (If the user wants another root, they have to pick option 4 again.) Use 100 iterations for the computation.

When option #5 is chosen, you are to compute the hyperbolic sine of a value, sinh(x). Here’s the formula:

sinh(x) = (ex – e-x)/2

You can compute this using the exponential function from option 2.

Thoughts: For this assignment, you are to use the switch-case statement to branch to the various choices for the first menu. You are forbidden to use <cmath> (which is the same as <math.h>) for this assignment!

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()



return 0;


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

When you submit: As usual, when you submit, you are all to enter the same information so as not to drive the graders crazy. Following these steps:

option 1 x = 5

option 3

x = 1 using 8 terms option 4

x = 60 find the fifth root

option 4 again for x = 729 find the 3rd root option 5

x = 2

option 2 x = 4


And, as always, let your TAs or instructor know if you need any help.

error: Content is protected !!