Program not giving any error/return value/output, help please!

This is a discussion on Program not giving any error/return value/output, help please! within the C Programming forums, part of the General Programming Boards category; Hello, This program that is supposed to find pi, and it should approximate pi to some number of terms. This ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    14

    Question Program not giving any error/return value/output, help please!

    Hello,
    This program that is supposed to find pi, and it should approximate pi to some number of terms. This number is an input from user. However, for some reason, when I run it and enter 10 as my input, it doesn't do anything. No error, no return. Here is a screenshot of what I am stuck at.
    Name:  Untitled.jpg
Views: 242
Size:  18.5 KB

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    long double approx1 (int terms);
    long double approx2 (int terms);
    int main ()
    {
       long double pi, first, second;
       int terms;
       char ch;
    
    
        printf ("\nEnter the number of terms to approximate pi, or 0 to terminate: ");
    	scanf ("%d", &terms);
    
    
    
    
    	while (terms != 0)
    	  {
    		 if (terms == 1)
    		 {
    		    pi = 4;
                printf ("\nFirst approximation of pi to 1 term is %.8Lf\n\n", pi);
                pi = sqrt(12);
    			printf ("Second approximation of pi to 1 term is %.8Lf\n\n", pi);
    		 }
    
    
    		 else if (terms > 1 )
    		 {
    		    first = approx1 (terms);
    			printf ("\nFirst approximation of pi to %d term is %.8Lf\n\n", terms, first);
    
    
    			second = approx2 (terms);
    			printf ("Second approximation of pi to %d term is %.8Lf\n\n", terms, second);
    
    
    			terms = 0;
    		 }
    		 else
    		 {
    		    printf ("\nWrong input, please try again\n\n");
    		 }
    		 printf ("Enter the number of terms to approximate pi, or 0 to terminate: ");
    		 scanf ("%d", &terms);
    	  }
    
    
        printf ("\n\n***** Program Terminated *****\n");
    
    
    	return 0;
    }
    
    
    long double approx1 (int terms)
    {
       int i;
       long double pi=0,denom=1.0,num=4.0;
    
    
       for ( i =1; i = (terms+1); i++)
       {
           if ((i % 2) != 0)
    	   {
    	      pi = pi + (num / denom);
    	   }
    
    
    	   else
    	   {
    	      pi = pi - (num / denom);
    	   }
    
    
    	  denom = denom + 2.0;
    	}
    
    
    	return pi;
    }
    
    
    long double approx2 (int terms)
    {
       int i;
       long double pi=0.0,denom = 1.0,num=12.0;
    
    
       for ( i =1; i = (terms+1); i++)
       {
           if ((i % 2) != 0)
    	   {
    	      pi = pi + (num / denom);
    	   }
    
    
    	   else
    	   {
    	      pi = pi - (num / denom);
    	   }
    
    
    	  denom = (denom + 1.0)*(denom +1.0);
    	}
    	pi = sqrt(pi);
    	return pi;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your re-assignment of i, in the second term of the for loop, is incorrect. Perhaps you meant i < , instead of i = ?

    Code:
    for ( i =1; ]i = (terms+1); i++)

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    so I can't use "i = (terms+1)" as the ending condition for a for loop?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by jackel View Post
    so I can't use "i = (terms+1)" as the ending condition for a for loop?
    maybe you do not remember that one equal stands for assignment.Two equal signs stand for comparisson.
    Example
    Code:
    ..
    int x=5;/*assign value 5 to x*/
    if(x==5)
    {
        printf("x is equal to five.Here is the proof -> x = %d\n",x);
    }
    else
    {
        printf("x is NOT equal to five.Here is the proof -> x = %d\n",x);
    }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    I just changed the i=(terms +1) to i<=terms, but it doesn't help.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    I think the source of the problem is my "long double approx1 (int terms)" function and the for loop. I checked the code again and again and can't find the error.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I guess you did not make the modification of the condition in for loop in both functions.
    And yes the problem is that you were trapped in an infinite loop.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jackel View Post
    I just changed the i=(terms +1) to i<=terms, but it doesn't help.
    The awful truth is, making one change, will not a working program make. Unless it's the last change needed, of course.

    I recognize the algorithm for Pi, but a few of the lines of code are looking a bit strange to me. Post up the exact equation you're using so us rusty Pi coders can get refreshed, and here's what I want you to do:

    1) At the start and just before the end of each function, add a printf() line of code, followed by a getchar(). You should print up the inputs to each function, at the top of them, and the output at the bottom of them.

    You need to verify that your variables are what they should be, and double check your calculations and logic.

    2) Post up the equation you're using. (there are several repeating fractions algorithms for Pi)

    And welcome to the "wonderful" world of debugging code!

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Ok, I finally fixed it. The issue was that I used "terms" as a variable in both my main () and approx1(int terms), so I changed "terms" in my main function to n, and now it works fine. So the compiler is somehow confused by it. Nonetheless, thank you to all of your responses.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Quote Originally Posted by Adak View Post
    The awful truth is, making one change, will not a working program make. Unless it's the last change needed, of course.

    I recognize the algorithm for Pi, but a few of the lines of code are looking a bit strange to me. Post up the exact equation you're using so us rusty Pi coders can get refreshed, and here's what I want you to do:

    1) At the start and just before the end of each function, add a printf() line of code, followed by a getchar(). You should print up the inputs to each function, at the top of them, and the output at the bottom of them.

    You need to verify that your variables are what they should be, and double check your calculations and logic.

    2) Post up the equation you're using. (there are several repeating fractions algorithms for Pi)

    And welcome to the "wonderful" world of debugging code!
    I don't quite understand what 1) does. Is this a way to verify my variables. My calculations and logic are right, and my only problem is using the right variables. My equations are pi = 4 - 4/3 + 4/5 - 4/7 +...., and pi^2 = 12 (1 - 1/4 + 1/9 - 1/16 +.....

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Thanks for the welcoming

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    If you have time and want to see another implementation take a look here Approximating PI using the Taylor Series - Help!

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Just looked at that link, but I don't understand the codes you guys post at all.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by jackel View Post
    Just looked at that link, but I don't understand the codes you guys post at all.
    well ok..this was the code we wrote when we started coding
    Code:
    /* File: picomp.c */
    #include <stdio.h> /* Header file for standard I/O library */
    #include <math.h> /* Header file for math library */
    int main(void)
    { 
         long i;
         double sum, current, pi;
         i = 1; /* Denominator of current term */
        sum = 0.0; /* So far sum */
       do {
           current = 1/(((double) i)*((double) i)); /* Current term */
           sum = sum+current; /* Add current term to sum */
           i++; /* Next term now */
       } while (current > 1.0e-15); /* Stop if current term is very small */
       pi = sqrt(6*sum); /* Compute approximation of pi */
       printf("Summed %8ld terms, pi is %10.8f\n", i-1, pi);
    }
    I posted the code in order you to get another perspective of it
    Do not even think of copy pase the code ,this will be bad for you and very sad for me :/ :/

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jackel View Post
    I don't quite understand what 1) does. Is this a way to verify my variables. My calculations and logic are right, and my only problem is using the right variables. My equations are pi = 4 - 4/3 + 4/5 - 4/7 +...., and pi^2 = 12 (1 - 1/4 + 1/9 - 1/16 +.....
    Why are you using two equations for this? Are you comparing them for converging efficiency?

    I'm used to seeing the older and slower first one, which I prefer expressed as:

    Pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 ...)

    Which converges slowly, but the loop for it is SO EASY to make. For less than 25 digits, it should be excellent.

    The second equation, I'm not sure what the divisor should be after the 16. Are you adding 5 to it, for each new factor in the series?

    Where did you get this second equation from, and why are you using it?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is this giving me this output?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-16-2012, 12:51 PM
  2. Replies: 4
    Last Post: 12-23-2011, 07:56 AM
  3. Replies: 4
    Last Post: 07-07-2011, 02:33 AM
  4. output screen isn't giving proper result
    By time4f5 in forum C Programming
    Replies: 11
    Last Post: 03-22-2011, 01:34 AM
  5. structure giving weird output
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-14-2010, 10:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21