C Programmers- Help a noob in C

FastAttack

Contributor
Veteran XX
I hate C, and I been forced to take it as part of my major (C for engineers) ,

Anyhow I have to write a quick program C to calculate PI

The program is this way

Write an interactive program that asks the user how many terms of the series equation to use in approximating pie. Then calculate and display the approximation.

The forumula they are talking about is

pi = 4X ( 1-1/3+1/5-1/7+1/9-1/11+1/13-....)

and so on.

Here is the example of the output they want:

Pi Approximation Program

How Many terms of the series should be included?
(The more terms, the better the approximation.)
=> 3
Approximate value of pi is 3.4667

what do I have so far? a non working version of the program that my small mind can't comprehend since C is a extra terrestial language.

PHP:
#include<math.h>
//main
int main()


    {
    	double sum;
    	int num;
    	int i;

        	{
        		printf("Pi Approximation Program\n");
        		printf("How Many terms of the series should be included?\n");
        		printf(" (The more terms, the better the approximation.)\n");
        		scanf("%lf", &num);
        	}
        	
        	while(num<=0);
        	sum=0;					//needed to sum terms
        	for(i=1;i<=num;i++)


            	{
            		if(i==1)			//special case for first term
            			sum = sum+1;		
            		if(i%2==0)			//subtract even terms
            			sum = sum-(1/((i*2.0)-1));	//multiply by 2.0 to force real number division
            										//otherwise it is int/int and sum of series is 
            										//nowhere near pi
            		if(i%2!=0 && i!=1)	//add odd terms except for 1
            			sum = sum+(1/((i*2.0)-1)); //multiply by 2.0 to force real number division
            										//otherwise it is int/int and sum of series is 
            										//nowhere near pi
            	}
            	printf("Pi is approximately: %f ",sum*4);
            	system("PAUSE");
            	return 0;
        }

this would be a lot easier in C++ since I have a strong idea how it works.

But of course I am lost with this problem
 
while(num>=0) i think might help

edit: im not sure whether // works in normal C, maybe /* comment */ would be better.
 
Last edited:
borlaK said:
// works fine in C

looking at the rest.. niece is bothering me :p

p.s. all true code warriors use C!
snow I did exactly what you said, the program just hangs :(

dam I hate this, I am guessing thats why I never wanted to be a programmer :)
 
of course it hangs, you put an infinite loop:

while(num<=0);

that would hang in c++ too
 
What Somec said.

You've said "while the number is >=0 um, don't do anything...." then you've finished the loop so the program just sits there.

You need to put all the code after the while(num>=0) in {}

That should start you off.

Next, please learn how to format your code :)
 
Last edited:
my only other guess is that when it is updating sum its not likeing the fact that you are using i which isnt a float.

also when you scan in you use %lf, but to print only %f
 
you can take that while loop out and just use the for loop

in fact, you don't decrement or increment num at all... maybe that while was temporary code that you forgot to take out?
 
Somec said:
you can take that while loop out and just use the for loop

in fact, you don't decrement or increment num at all... maybe that while was temporary code that you forgot to take out?
I think I might've , I am not the best programmer in the world ( as seen here) I am kinda following my book.

I took out the while loop , but now everything comes out at zero.

the question is my logic correct as far as getting the answer?
 
1/((i*2.0)-1));

You're going to end up doing integer division there (1/something). Use 1.0f instead of 1. You also might want to cast i as a double to be safe.
 
Somec said:
of course it hangs, you put an infinite loop:

while(num<=0);

that would hang in c++ too
Yep.

While is executing the next statement which ends at the first ";". Which means nothing is being executed and the test in while is being repeated indefinitely.

Most modern compilers should give a warning on something like this, unless you're on UNIX or something.
 
NutWager said:
Most modern compilers should give a warning on something like this, unless you're on UNIX or something.
Is that supposed to imply there are no "modern" compilers for *nix? :p
 
Code:
...

double sum;
double term;
int i,s;

sum = 0.0;
term = 1.0;
s = 1;

for(i=0;i<num;i++) {
   sum  = sum + s*(1.0/term);
   s = -s;
   term = term+2;
}
...
 
Back
Top