Im using DEV C++ (Bloodshed?) In this project you will need to implement at leas
ID: 3536935 • Letter: I
Question
Im using DEV C++ (Bloodshed?)
In this project you will need to implement at least three (3)
functions. One of them should be void while the other two with a
suitable return type. The part of the code, that you wish to
implement as part of your structured algorithm, is left to you. One
more thing: notice that when trying to quit now, there is a
precaution question that asks you if you want to quit or not. This
approach is used a lot in order to prevent users from accidentally
quitting (you must be sure that you want to quit in order to press
both zero and enter y after that!). Also, the user is not allowed
to calculate anything until they input some data first. Similarly,
once the user has given data, they need to use the modify option
(no. 4 in the menu) in order to do so.
For the program you will
have to submit you will only need the two following Taylor series:
sine and cosine. Note that you will have to use the factorial
function on both of the trigonometric approximations that you will
make. It might be a good idea to incorporate the factorial in one
of the functions that you will implement!
Also, you will need to
report the %u201Ctrue%u201D values of the sine and the cosine as well as the
%u201Capproximate%u201D ones. In order to do so, you will have to include the
header file and use the functions sin(x) and cos(x).
SAMPLE OUTPUT:
MAIN MENU
1. To enter the data.
2. To calculate and approximate the
sin(x)
3. To calculate and approximate the cos(x)
4. To modify
data. Please make a choice: 2
You have to enter a value first!
MAIN MENU 1. To enter the data. 2. To calculate and approximate the
sin(x) 3. To calculate and approximate the cos(x) 4. To modify
data.
Please make a choice: 5
Wrong choice. Only options 1-4 are
available.
MAIN MENU 1. To enter the data. 2. To calculate and
approximate the sin(x) 3. To calculate and approximate the cos(x)
4. To modify data.
Please make a choice: 1
Please give a value for the angle: 1.37
Please give a value for the approximation order n: 6
MAIN MENU 1. To enter the data. 2. To calculate and approximate
the sin(x) 3. To calculate and approximate the cos(x) 4. To modify
data.
Please make a choice: 2
The sine of x is: 0.979908
Using Taylor series it is: 0.979908
MAIN MENU 1. To enter the data. 2. To
calculate and approximate the sin(x) 3. To calculate and
approximate the cos(x) 4. To modify data.
Please make a choice: 3
The sine of x is: 0.19945 Using Taylor series it is: 0.19945 MAIN
MENU 1. To enter the data. 2. To calculate and approximate the
sin(x) 3. To calculate and approximate the cos(x) 4. To modify
data.
Please make a choice: 1
Please use the modify option to enter
a new angle.
using primerily functions, loops, and boolean statments,
*please explain how you wrote the code/ why you used
certain things and what not so I can do it on my own. Thank you!
My code so far:
//(right now im trying to get it so the menu has a case for Input of 1, so it can continue with options 2,3,4 when data is input)
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
char x;
do{
cout<< "MAIN MENU"<<endl;
cout<< "1. To enter the data." <<endl;
cout<<"2. To calculate and approximate the sin(x)"<<endl;
cout<<"3. To calculate and approximate the cos(x)"<<endl;
cout<<"4. To modify data."<<endl;
cout<<"0. To quit." <<endl;
cout<<"Please make a choice: " <<endl;
cin>>n;
while(!(cin>>n)){
cout << "Wrong choice. Only options 1-4 are available."<< endl;
cin.clear();
cin.ignore(1000, ' ');
}
if (n==0){
cout << "Are you sure you want to quit? (Y/N): "<< endl;
cin>>x;
if (x =='Y' || x== 'y') break;
}
}while (n!=1);
system ("PAUSE");
return 0;
}
/* (factorial for denom of series)
long int fact(long int n)
{
int f;
for ( f = n - 1 ; f > 1 ; f-- )
{
n *= f;
}
return n;
}
*/
Explanation / Answer
/* do comment on the answer if any doubts ... */
#include <iostream>
#include <cmath>
using namespace std;
double approxsin(double x,int i)
{
/* caluclate every term by multiplying factors to the previous term
no need for actorial function then */
double term = x;
double sum = x;
for(int n=1; n<i; ++n)
{
term *= x*x / ( 2*n * (2*n+1) );
/* choose sign for even/odd terms */
sum += n%2 ? -term : term;
}
return sum;
}
double approxcos(double x,int i)
{
/* caluclate every term by multiplying factors to the previous term
no need for actorial function then */
double term = 1;
double sum = 1;
for(int n=1; n<i; ++n)
{
term *= x*x / ( 2*n * (2*n-1) );
/* choose sign for even/odd terms */
sum += n%2 ? term : -term;
}
return sum;
}
int main(){
int choice;
char x;
int check=0;
double angle;
int n;
while(1)
{
cout<< "MAIN MENU"<<endl;
cout<< "1. To enter the data." <<endl;
cout<<"2. To calculate and approximate the sin(x)"<<endl;
cout<<"3. To calculate and approximate the cos(x)"<<endl;
cout<<"4. To modify data."<<endl;
cout<<"0. To quit." <<endl;
cout<<"Please make a choice: " <<endl;
cin>>choice;
if (choice == 0 )
{ break; }
if(choice<1 || choice>4)
{
cout<<"Wrong choice. Only options 1-4 are available"<<endl;
continue;
}
if(choice == 1)
{
if(check==1)
{ cout<<"Please use the modify option to enter a new angle"<<endl;
}
else
{
cout<<"Please give a value for the angle:"<<endl;
cin>>angle;
cout<<"Please give a value for the approximation order n:"<<endl;
cin>>n;
check =1;
}
continue;
}
if(choice == 2)
{
if( check == 0 ) { cout<<"You have to enter a value first! "<<endl; }
else {
cout<<"The sine of x is:"<<sin(angle)<<endl;
cout<<"Using taylor series it is: "<<approxsin(angle,n)<<endl;
}
continue;
}
if(choice == 3)
{
if( check == 0 ) { cout<<"You have to enter a value first! "<<endl; }
else {
cout<<"The sine of x is:"<<cos(angle)<<endl;
cout<<"Using taylor series it is: "<<approxcos(angle,n)<<endl;
}
continue;
}
if(choice ==4)
{
if( check == 0 ) { cout<<"You have to enter a value first! "<<endl; }
else {
cout<<"Please give a value for the angle:"<<endl;
cin>>angle;
cout<<"Please give a value for the approximation order n:"<<endl;
cin>>n;
check =1;
}
continue;
}
}
/* system ("PAUSE"); */
return 0;
}