Thread: program doesnt enter function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    program doesnt enter function

    hello everyone, i wrote a code in C that suppose to calculate COS of an angle.
    i dont need help with the writing, i just have one problem, for some reason the program doesnt enter the function cosApprox, it suppose to enter the function and save the result in sum_column , but it just skips it, does someone knows why?

    Thanks.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    #define PI 3.1415927
    
    double power(double a, int b);
    
    long factorial(int n);
    
    double cosElement(double x, int i);
    
    double cosApprox(double x, int i);
    
    int main()
    {
    	 double x;  /* Angle variable */
    	 int k;     /* Number of elements variable */
    	 double sum_column; /* Sum of all elements variable */
    
    	 clrscr();
    
    	 printf("\nEnter an angle (in Radians):");
    	 scanf("%lf", &x);
    	 printf("\nEnter number of elements:");
    	 scanf("%d", &k);
    	 while(k<0)
    	 {
    		printf("Erroneous Input. Please enter number of elements (k>=0):");
    		scanf("%d", &k);
    	 }
    
    	 sum_column=cosApprox(x, k); /* a function to calculate the sum of elements */
    
    	 printf("The value of Cos(x) is: %.4lf", 1+sum_column);
    
    	 getch();
    	 return 0;
    }
    	 double cosApprox(double x, int i)
    	 {
    		 double sum=0;
    
    		 for(i=i;i>0;i--)
    		 {
    
    			 sum=sum+cosElement(x, i);
    		 }
    		 return sum;
    	 }
    	 double cosElement(double x, int i) /* a function to calculate the element in the i place of the column */
    {
    	 double element;
    	 element=pow((-1),i)*power(x,i)/factorial(i);
    
    	 return element;
    }
    
    
    	 long factorial(int n)  /* a function to calculate the factorial of a number */
    {
    	 long sum_f=2*n;
    	 int i;
    
    	 for(i=2*n;i>1;i--)
    	 {
    		 sum_f=sum_f*(2*n-1);
    	 }
    	 return sum_f;
    }
    
    	 double power(double a, int b) /* a function to calculate x^2k */
    {
    	 double power;
    
    	 power=pow(a,2*b);
    
    	 return power;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's hard to say why it skips it, given that it does not, in fact, skip the function at all, but instead executes it.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's God punishing you for your indentation style.

    Seriously, it's going there - your logic is bad, but it's going there. Math is above my pay grade, but it's stepping nicely right into cosApprox().

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    i used the debugger and it totally ignored the function, it just returned a garbage value and didnt enter the function.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You pressed F8, instead of F7.

    F8 skips over going into the function. F7 will follow the code, into any function.

    Try again, it's going there. I wouldn't lie.

    OK, I might lie, but only a little white lie, not a big programming lie.

    OK, would you believe I might lie a big black lie, but I'd feel bad about it, and wouldn't do it about programming, anyway.

    OK, would you believe ...

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    yes, Adak is right.the program is entering the function.when i executed the code , it's also giving the answer.for eg,
    for angle(in radians)=1.0476(PI/3), k=7
    cos(x)=0.4623.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 07:38 PM