Solution
//Programmer: Clayton Price
//File: math.cpp
//Purpose: This file contains the main function for a program that will help
// the user compute various math values.
#include <iostream>
using namespace std;
int main()
{
/* ------------------ DECLARATIONS OF CONSTANTS AND VARIABLES ---------- */
const short MIN_EXP_VAL = 0; //smallest value to exponentiate
const short MAX_EXP_VAL = 7; //greatest value to exp
const short NUM_TERMS_EXP = 6; //number of terms for calc'ing exp(x)
const short MIN_ACC_SIN = 1; //least num terms for calc'ing sin(x)
const short MAX_ACC_SIN = 5; //greatest num terms for same
const short MAX_ITERATIONS = 20; //number of iterations for newton's
// method of computing roots
char choice; //user input choice
char root_choice; //menu choice for roots
bool quit = false;
bool x_val_entered = false; //for roots menu
bool square_root_done = false; //have calc'd sq root?
bool cube_root_done = false; //have calc'd cube root?
float x_val; //calculating values
float next_x;
double last_term;
double exp_x;
double sin_x;
short num_terms;
/* ---------------------------GREETINGS AND INPUTS------------------------ */
cout << "\n\n\n\n\n\tWelcome to MATH-O-MATICS..."<<endl<<endl;
do
{
cout<<" YOUR OPTIONS"<<endl<<endl
<<" ------------"<<endl
<<"\t1. Exp(x)"<<endl
<<"\t2. sin(x)"<<endl
<<"\t3. roots of x"<<endl
<<"\t4. quit and run away!"<<endl<<endl
<<"\tYour choice: ";
cin>>choice;
/* --------------------------- ACT UPON INPUTS ------------------------- */
switch (choice)
{
case '1': case 'e': //computing exp(x)
do
{
cout << "Please enter a value x to exponentiate: ";
cin >> x_val;
if (x_val > MAX_EXP_VAL || x_val < MIN_EXP_VAL)
{
cout<<"Please limit input to be between and including "
<< MIN_EXP_VAL<<" and "<< MAX_EXP_VAL<<": ";
cin>>x_val;
}
} while (x_val < MIN_EXP_VAL || x_val > MAX_EXP_VAL);
//input good....calc exp(x) using MacClaurin series
exp_x = 1; //base case
last_term = exp_x; //base case - start point
for(int i = 1; i < NUM_TERMS_EXP; i++)
{
last_term *= (x_val/i); //create next term
exp_x += last_term; //add to the approx.
}
cout<<"exp("<<x_val<<") is = "<<exp_x<<endl<<endl;
break;
case '2': case 's': //computing sin(x)
cout<<"Please enter the value to take sine of: ";
cin>>x_val;
do
{
cout<<"Now, enter an integer between and including "<<MIN_ACC_SIN
<<" and "<<MAX_ACC_SIN<<": ";
cin>>num_terms;
} while (num_terms < MIN_ACC_SIN || num_terms > MAX_ACC_SIN);
sin_x = x_val; //base case
last_term = sin_x; //base case latt term
for(int i = 1; i < num_terms; i++)
{
last_term *= -1 * (x_val/(2*i))*(x_val/(2*i+1));
sin_x += last_term;
}
cout<<"sin("<<x_val<<") is = "<<sin_x<<endl<<endl;
break;
case '3': case 'r': //calculating roots
do
{
cout<<"\n\n ROOT OPTIONS"<<endl<<endl
<<"\t1. enter value to rootify"<<endl
<<"\t2. square root"<<endl
<<"\t3. cube root"<<endl<<endl
<<"\t\tYour choice: ";
cin>>root_choice;
switch (root_choice)
{
case '1': case 'e':
do
{
cout<<"enter x value for root: ";
cin>>x_val;
} while (x_val < 0);
x_val_entered = true;
square_root_done = false;
cube_root_done = false;
break;
case '2': case 's':
if (!x_val_entered) //error condition
cout<<"\nERROR: must enter x value first..."<<endl<<endl;
else
{ // **** Newton's method for 2nd root
next_x = x_val;
for (int i = 1; i <= MAX_ITERATIONS; i++)
next_x = (next_x + x_val/next_x)/2.0;
cout<<"\n\n***The square root of "<<x_val<<" is "<<next_x
<<endl<<endl;
square_root_done = true;
}
break;
case '3': case 'c':
if (!x_val_entered) //error condition
cout<<"\nERROR: must enter x value first..."<<endl<<endl;
else
{ // **** Newton's method for 3rd root
next_x = x_val;
for (int i = 1; i <= MAX_ITERATIONS; i++)
next_x = (2*next_x + (x_val/(next_x * next_x)))/3.0;
cout<<"\n\n***The cube root of "<<x_val<<" is "<<next_x
<<endl<<endl;
cube_root_done = true;
}
break;
default:
cout<<"\nERROR: invalid choice....."<<endl;
} //end switch for roots
if (x_val_entered && !square_root_done)
cout<<"\n****must calculate square root"<<endl;
if (x_val_entered && !cube_root_done)
cout<<"\n****must calculate cube root"<<endl;
} while (!x_val_entered || !square_root_done || !cube_root_done); //end do-while for roots menu
x_val_entered = false; //reset flags for next time root chosen
square_root_done = false;
cube_root_done = false;
break;
case '4': case 'q':
quit = true;
break;
default:
cout << "\n*****ERROR: invalid choice....try again Einstein!*****\n"
<<endl;
} //end switch
} while (!quit);
cout << "later gator ........... and good luck with math!" << endl;
return 0;
}