Thread: Program prints out wrong result

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Program prints out wrong result

    Write a function that will for an angle x expressed in radians
    calculate the approximate value of the sine function as a partial
    sum of n terms of the sequence . The function receives as arguments real
    number x and a positive integer n. (Do not use header <math.h> ! )


    http://s33.postimg.org/c1t7ao7mn/sin_Aprox.jpg
    Code:
    #include <stdio.h>
    #define pi 3.14159265358979323846
    
    
    int fakt( int n){
    	int i, fakt = 1;
    	for( i = 1; i <= n; i++){
    		fakt *= i;
    	}
    	return fakt;	
    }
    float degToRad( float angle1){
    	float rad;
    	rad = angle1 * (pi/180.0);
    	return rad;
    }
    float pow( float base, int exponent ){
    	float result = 1;
    	int i;
    	for( i = 1; i <= exponent; i++){
    		result *= base;
    	}
    	return result;
    }
    float sinus( float deg, int numOfSeries){
    	float sinusCalc = 0, angle, denominator, nominator;
    	int i = 1, numOfDenom;
    	angle = degToRad(deg);
    	for( i = 1; i <= numOfSeries; i++){
    		/*numOfDenom = 2 + i - 1;
    		denominator = fakt(numOfDenom);
    		nominator = pow(angle,numOfDenom);
    		sinusCalc += pow(-1.0, i+1) * ( denominator/nominator); this aprroach printed out some crazy numbers*/
    		sinusCalc += pow(-1.0, i+1) * ( pow(angle, (2*i-1) ) / fakt( 2*i - 1) ); 
    	}
    	return sinusCalc;
    }
    int main(void) {
    	float angle,resultAngl, resultSinus;
    	int n;
    	printf("Enter angle in degrees and number of series: ");
    	scanf("%f %d" ,&angle, &n);
    	resultSinus = sinus(angle, n);
    	printf("%f", resultSinus);	
     	return 0;
    }
    Tried to check functions fakt(),degToRad(),pow()and they are all working but in function sinus(); for example for angle = 90 degrees , and for i = 1 the result should be 1.57 and it stored 0.810569, not quite sure why this is happening.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The answer is to debug your program and step through it to see where it goes wrong.
    Eg.
    Code:
    $ gdb -q ./a.out 
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) b 34
    Breakpoint 1 at 0x400649: file foo.c, line 34.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    Enter angle in degrees and number of series: 45 3
    
    Breakpoint 1, sinus (deg=45, numOfSeries=3) at foo.c:34
    34	    sinusCalc += pow(-1.0, i+1) * ( pow(angle, (2*i-1) ) / fakt( 2*i - 1) );
    (gdb) print angle
    $1 = 0.785398185
    (gdb) print i
    $2 = 1
    (gdb) print pow(-1.0, i+1)
    $3 = 1
    (gdb) print pow(angle, (2*i-1))
    $4 = 0.785398185
    (gdb) print fakt( 2*i - 1)
    $5 = 1
    (gdb) c
    Continuing.
    
    Breakpoint 1, sinus (deg=45, numOfSeries=3) at foo.c:34
    34	    sinusCalc += pow(-1.0, i+1) * ( pow(angle, (2*i-1) ) / fakt( 2*i - 1) );
    (gdb) print pow(-1.0, i+1)
    $6 = -1
    (gdb) print pow(angle, (2*i-1))
    $7 = 0.484473109
    (gdb) print fakt( 2*i - 1)
    $8 = 6
    (gdb) c
    Continuing.
    
    Breakpoint 1, sinus (deg=45, numOfSeries=3) at foo.c:34
    34	    sinusCalc += pow(-1.0, i+1) * ( pow(angle, (2*i-1) ) / fakt( 2*i - 1) );
    (gdb) print sinusCalc
    $9 = 0.704652667
    (gdb) delete 1
    (gdb) c
    Continuing.
    0.707143[Inferior 1 (process 5236) exited normally]
    In just 3 iterations, the answer is right to 4 decimal places.

    What do you see in your test case which 'fails'?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 02-10-2015, 12:06 PM
  2. Replies: 3
    Last Post: 01-28-2015, 10:53 AM
  3. Confusing pointers, Prints wierd result
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 06-29-2010, 03:35 AM
  4. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM

Tags for this Thread