PROGRAM MUST BE IN C. DO NOT POST CODE FOR C++, JAVA, OR ANY OTHER LANGUAGE AND
ID: 3804798 • Letter: P
Question
PROGRAM MUST BE IN C.
DO NOT POST CODE FOR C++, JAVA, OR ANY OTHER LANGUAGE AND SAY CONVERT IT TO C. ONLY C PLEASE. THERE IS ANOTHER CODE FOR THIS PROGRAM POSTED IN C++. DO NOT POST THAT AGAIN.
THE TWO FUNCTIONS MAIN() AND UNSIGNED INT FACTORIAL (INT N) MUST BE PRESENT. PLEASE USE DATA TYPE UNSIGNED INT AS RETURN VALUE FOR FACTORIAL FUNCTION.
PLEASE ALLOW FOR 6 DIGITS PRECISION. THANKS.
For this program, let's calculate the Taylor series expansion for the exponential function: e The equation we will implement is: This summation will converge to ex after some number of terms have been added to the whole. The number of terms will vary depending on the value of x. Note that every term in the summation is positive, so the summation will converge to the actual value from below. "x" is assumed to be 0 In general, if we wish to test two floating point numbers for equality, we can calculate the absolute value of the difference between the numbers and then determine if that difference is less than some epsilon value. Let's apply that technique to this problem. We'll test the growing sum against the standard e value (as calculated by the exp (x) function) until the difference between the two becomes less than some epsilon (to be defined shortly). At that point we'll consider that the series has converged on the correct value. In addition, we'll track the number of terms that must be added to the sum to achieve convergence and report that value to the user Functions Your program should contain at least two functions:Explanation / Answer
include<stdio.h>
#include<math.h>
#define PI
float exp_x( int, int );
int fact( int );
int main()
{
int choice, x, n;
do
{
printf( " Menu [1] e^x) [2] Sin(x) [3] Cos(x) [4] Exit " );
scanf( "%d", &choice );
switch ( choice )
{
case 1: // to find exponential value
printf( "e^x Enter x and n: " );
scanf( "%d %d", &x, &n );
printf( "e^%d(%d) = %f ", x, n, exp_x( x, n ) );
break;
case 2: // to find sinx
printf( "sin(x) Enter x and n: " );
scanf( "%d %d", &x, &n );
printf( " cos(%d)(%d) = %f ", x, n, sin_x( x, n ) );
break;
case 3: // to find cosx
printf( "cos(x) Enter x and n: " );
scanf( "%d %d", &x, &n );
printf( " cos(%d)(%d) = %f ", x, n, cos_x( x, n ) );
break;
case 4: // exit
break;
default: // wrong choice
printf( "Wrong choice" );
break;
}
}
while ( choice != 4 );
}
float exp_x( int x, int n )
{
int i = 1;
float ex = 1;
while ( i < n )
{
ex += ( float ) pow( x, i ) / fact( i );
++i;
}
return ex;
}
double sin_x( int ang_deg, int no_of_terms )
{
int term, j;
double value = 0.0, ang_rad = 0.0;
ang_rad = ( double ) ang_deg * PI / 180;
for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
{
value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
}
return value;
}
double cos_x( int ang_deg, int no_of_terms )
{
int term, j;
double value = 1.0, ang_rad = 0.0;
ang_rad = ( double ) ang_deg * PI / 180;
for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
{
value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
}
return value;
}
int fact( int num )
{
int f = 0;
if ( num == 1 )
return 1;
else
f = num * fact( num - 1 );
return f;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.