Question: C code needs to be fixed and debugged. Not sure what is giving the error. If anyone could fix it and be able to compile
C code needs to be fixed and debugged. Not sure what is giving the error. If anyone could fix it and be able to compile and run it with the corrected parts it would be appreciated.
Error showing up is for double sin_x (error implicit declaration of sin_x) There may be other errors too. Please fix those as well.
#include
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:\t" ); 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:\t" ); 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:\t" ); 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;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
