Hi! I have written a part of the program but don't know if I'm going in the right direction. I was also wondering how to use getchar() because I've never used that before. I'm quite a novice at this so any help would be greatly appreciated. Thanks!
Scan a double variable x and evaluate y=exp(x) by
using the math.h library of functions.
Your objective in this homework is to compare the so calculated
value of y with the approximate value Y obtained by
using 1, 2, 3, 4, and 5 leading terms of the Taylor series:
exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + x^4/4! + ....
Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 and 0!=1.
Use a do/while loop in conjunction with a switch statement
switch(). Enter a character 1 for one term approximation,
2 for two terms, etc., by using getchar() function. Use
0 to exit the program. Within switch, use default:
if (char != '0') => unrecognized operator.
Let your opening case calculate the fourth term x^4/4!, let the
subsequent case calculate the x^3/3! term, etc. (In this way,
you wan't need to use repeatedly a break statement within your switch).
Evaluate the corresponding Y, print the result in one line,
and the relative error (Y-y)/y in another line.
Execute your program for all five cases (with corresponding
printouts).
............................................
Your output should be like this:
Enter x: 1.25
True value of exp(1.2500) = 3.490343
Enter a character 1-5 (0 to exit):
1
1 term(s) approximation
Approximate exp(1.2500) = 1.000000
Relative error = -71.349520 percent
Enter a character 1-5 (0 to exit):
2
2 term(s) approximation
Approximate exp(1.2500) = 2.250000
Relative error = -35.536421 percent
Enter a character 1-5 (0 to exit):
3
3 term(s) approximation
Approximate exp(1.2500) = 3.031250
Relative error = -13.153233 percent
Enter a character 1-5 (0 to exit):
4
4 term(s) approximation
Approximate exp(1.2500) = 3.356771
Relative error = -3.826905 percent
Enter a character 1-5 (0 to exit):
5
5 term(s) approximation
Approximate exp(1.2500) = 3.458496
Relative error = -0.912428 percent
Enter a character 1-5 (0 to exit):
6
unrecognized operator
Enter a character 1-5 (0 to exit):
0
Code:
#include<stdio.h>
#include<math.h>
/* Scanning a double variable x and evaluating y=exp(x) by
using the math.h library of functions */
int main() {
/* Defining variables */
int k, N;
char ch;
double x, y, Y;
/* Two empty lines and data input + true value of exp(x) at beginning */
printf("\n\nEnter x:\n\n");
scanf("%lf", &x);
y=exp(x);
printf("True value of exp(%lf) = %9.6f\n", x, y);
/* Data input: do/while statement with a switch statement within */
do {
printf("Enter x:\n");
scanf("%d", &N);
y=0;
switch(N) {
case 5;
y-=
}
}
}