In this assignment you will write a C program to show a menu to the user to sele
ID: 3584966 • Letter: I
Question
In this assignment you will write a C program to show a menu to the user to select the function to evaluate.
The menu will look like:
Please select an option:
a) Evaluate exponential function
b) Evaluate sin function
c) Exit The only options valid are from a to c.
Background: The exp and sin ofx, can be obtained using power series 2 4 n=0 (-1)%2n+1 sin(x) 3! 5! n=0 Vexp - Vmathh 100 math.h Example: Since time is limited, we won't evaluate the series all the way to GO; instead we will ask the user for a number of terms to be considered, say t. For example if the user chose to evaluate the exponential function for x-0.5, and t-4: 0.53 0.54 exp(0.5)-eo s = 0S" = + 0.5 + 0212 + T + T = 1.648 n=0 Now, using the exp function in math.h: exp(0.5) -1.648721 We now calculate the %error using the formula: Vexp Vmath.h +100 math.h Where Vexp is the value obtained using the input parameters x and t, and Vmath.h is the value obtained using the math.h function us 1.6484 - 1.648721 1.648721 * 1001 0.0195 Using these values, the program should display a table like: result 1.6484 math.h 1.6487 error 0195 0.5 4Explanation / Answer
#include<stdio.h>
#include<math.h>
int t; // No. of terms
int menu()
{
char ch;
printf("a) Evaluate exponential function ");
printf("b) Evaluate sin function ");
printf("c) Exit ");
printf("================================= ");
printf("Enter choice [a - c ] : ");
scanf(" %c",&ch);
return ch;
}
double myexp(double x)
{
long int f=1;
double er=1;
int i;
printf("Enter no. of terms : ");
scanf("%d",&t);
for(i=1;i<=t;i++)
{
f=f*i;
er=er + (pow(x,i)/f);
}
return er;
}
double mysin(double x)
{
long int f=1;
int i,p = -1;
double sr=0;
printf("Enter no. of terms : ");
scanf("%d",&t);
for(i=1;i<=t;i++)
{
f=f*i;
if(i%2==1)
{
p=p*-1;
sr=sr+ ((x/f)*p);
}
}
return sr;
}
int main()
{
double x;
double er,er1; // er-t terms computed exp result , er1 - exp result by calling exp() of math.h
double sr,sr1; // sr - t terms computed sin result, sr1 - sin result by calling sin() of math.h
double errec,errsc; // errc - error exp correction, errsc - error sin correction
while(1)
{
switch( menu() )
{
case 'a':
case 'A':
printf("Enter x value : ");
scanf("%lf",&x);
er=myexp(x);
er1=exp(x);
errec= ((er - er1) / er1)*100;
printf("x t result math.h %Error ");
printf("%.2lf %2d %.4lf %.4lf %.4lf ",x,t,er,er1,fabs(errec));
break;
case 'b':
case 'B':
printf("Enter x value : ");
scanf("%lf",&x);
sr=mysin(x);
sr1=sin(x);
errsc= ((sr - sr1) / sr1)*100;
printf("x t result math.h %Error ");
printf("%.2lf %2d %.4lf %.4lf %.4lf ",x,t,sr,sr1,fabs(errsc));
break;
case 'c':
case 'C': return 0;
default: printf("invalid choice ... ");
}
}
}
/*
lenovo@lenovo-Vbox:~/chegg$ gcc expsin.c -lm
expsin.c: In function ‘main’:
expsin.c:66:20: warning: unknown conversion type character 0x1b in format [-Wformat=]
printf("x t result math.h %Error ");
^
expsin.c:76:20: warning: unknown conversion type character 0x1b in format [-Wformat=]
printf("x t result math.h %Error ");
^
lenovo@lenovo-Vbox:~/chegg$ ./a.out
a) Evaluate exponential function
b) Evaluate sin function
c) Exit
=================================
Enter choice [a - c ] : a
Enter x value : 0.5
Enter no. of terms : 4
x t result math.h %rror
0.50 4 1.6484 1.6487 0.0172
a) Evaluate exponential function
b) Evaluate sin function
c) Exit
=================================
Enter choice [a - c ] : b
Enter x value : 0.5
Enter no. of terms : 4
x t result math.h %rror
0.50 4 0.4167 0.4794 13.0904
a) Evaluate exponential function
b) Evaluate sin function
c) Exit
=================================
Enter choice [a - c ] : z
invalid choice ...
a) Evaluate exponential function
b) Evaluate sin function
c) Exit
=================================
Enter choice [a - c ] : c
lenovo@lenovo-Vbox:~/chegg$
*/