Question: #include #include / / Function Prototypes double calculate _ exp ( double x , int t ) ; double calculate _ sin ( double x

#include
#include
// Function Prototypes
double calculate_exp(double x, int t);
double calculate_sin(double x, int t);
double factorial(int n);
// Main Function
int main(){
// Program header
printf("Author: Your Name
");
printf("Class: Your Class
");
printf("Date: Today's Date
");
printf("Description: This program evaluates exponential and sine functions using power series.
");
char option;
double x, result, math_h, error;
int t;
// Display the menu
printf("Please select an option:
");
printf("a) Evaluate exp function
");
printf("b) Evaluate sin function
");
// Get the user's choice
scanf("%c", &option);
if (option =='a'|| option =='A'){
printf("Valid option
");
// Get user inputs for x and t
printf("Enter the value for x: ");
scanf("%lf", &x);
printf("Enter the number of terms t: ");
scanf("%d", &t);
// Calculate exp using power series
result = calculate_exp(x, t);
// Calculate exp using math.h
math_h = exp(x);
// Calculate the percentage error
error = fabs((result - math_h)/ math_h *100);
// Display the result
printf("%4.1lf %4d %6.4lf %6.4lf %6.4lf
", x, t, result, math_h, error);
} else if (option =='b'|| option =='B'){
printf("Valid option
");
// Get user inputs for x and t
printf("Enter the value for x: ");
scanf("%lf", &x);
printf("Enter the number of terms t: ");
scanf("%d", &t);
// Calculate sin using power series
result = calculate_sin(x, t);
// Calculate sin using math.h
math_h = sin(x);
// Calculate the percentage error
error = fabs((result - math_h)/ math_h *100);
// Display the result
printf("%4.1lf %4d %6.4lf %6.4lf %6.4lf
", x, t, result, math_h, error);
} else {
printf("Invalid option
");
}
return 0;
}
// Function Definitions
double calculate_exp(double x, int t){
double sum =1.0; // Start with 1(x^0/0!)
for (int n =1; n < t; ++n){
sum += pow(x, n)/ factorial(n);
}
return sum;
}
double calculate_sin(double x, int t){
double sum =0.0;
for (int n =0; n < t; ++n){
int coefficient = pow(-1, n); // Alternate between + and -
sum += coefficient * pow(x,2*n +1)/ factorial(2*n +1);
}
return sum;
}
double factorial(int n){
if (n ==0){
return 1.0;
}
double fact =1.0;
for (int i =1; i <= n; ++i){
fact *= i;
}
return fact;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!