C Programmers, please HELP with small program

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
So the goal of the program I am to write is: To write a program that finds a ratio of two small(less than 1000) integers that estiamtes PI to at least 0.00001%.

hints: use a double loop.

So far I got this:

#define PI 3.1415926535

int main(void) {
float i,j,r;
for(i=0; i<1000; i++);
for(j=0; i<1000;j++);
{
r=i/j;
}

system("PAUSE");
return 0;
}

But I really am not sure how I am suppose to bring in PI or really to go any further then what I just did. Please help guys. Thanks
 

RaiderJ

Diamond Member
Apr 29, 2001
7,582
1
76
Hmm... I think you're going to have to define pi with an external library, or at least to quite a few more decimal places (for a start). As for the ratio of two numbers, you could take the stupid/slow approach and set one double loop variable to the numerator, the other to the denominator. Have it go through each possibility, compare its value to pi, and if it matches your percentage, print i & j.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
compare r to PI inside the inner most loop, print out i and j?

is this a trick question?
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Im not sure if Im doing it right at all. But my teacher just said use a double loop and try all numerators and denominators. But you see, I not good at this programming stuff. So I'm trying to get all the advice I can get. But trick question, no..
 

blackllotus

Golden Member
May 30, 2005
1,875
0
0
You don't even need r. You just need an "if" like Ameesh said.

if (fabs(PI - i/j) < 0.000001) { ... }

Its easier to use some simple constant like "0.000001" instead of actually using 0.00001% of PI

EDIT: Btw, floats are outdated ;) Use doubles
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Ah so RaiderJ, I kind of understand what you are saying with taking the stupid/slow appraoch but could you give me an example of how to do it?
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
doubes, got it! BTW this is for my CS151 class. So I bet this stuff is very easy for you guys. Thanks for the help so far guys.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
ok so I just added some new things but I get a weird #, like 1000 for i and 0 for j. Heres my code:
#define PI 3.1415926535

int main(void) {
double i,j;
for(i=0; i<1000; i++);
for(j=0; i<1000;j++);
{

}
if (fabs(PI-i/j) < 0.0000001); {
printf("The value of i and j are: %0.2lf,%0.2lf\n", i,j);
}
system("PAUSE");
return 0;
}
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Why is your if statement outside of your inner For loop? J is always going to cycle from 0 to 999 at that rate :eek:
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
so now after that.. my values come out to be 1000, 1000 which isn't right at all. What is wrong with my code?
 

RaiderJ

Diamond Member
Apr 29, 2001
7,582
1
76
Your for loops don't do anything productive... just count to 1000. Do your comparison to PI on the innermost double loop.
 

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
I think you need to sit down and understand the concept of the for-loop. You get the idea that you're using it to increment i and j. The part you seem to be missing is that the for-loop let you repeat execution of a section of code inside the for-loop.

for (i=0; i<4; i++){
printf(i);
}

would print i until the condition i<4 is met.

It's not a difficult problem if you just think about it and apply the stuff you're supposed to be learning now.
 

CSMR

Golden Member
Apr 24, 2004
1,376
2
81
Your for and if statements are wrong, assuming my guess about C is right.

for(i=0,i<1000,i++);
{code(i);}

I imagine increments i up to 1000 and then runs code(1000).

Whereas
for(i=0,i<1000,i++)
{code(i);}
would run code(1) up to code(1000). The difference is a semicolon.

Same with if. That's why your printf statement is being run, because it is run regardless of the conditions you wanted to give it, because of the semicolon.
 

itachi

Senior member
Aug 17, 2004
390
0
0
'if(fabs(PI - i/j) < 1.0e-7)' is wrong.. he needs to compare the ratio of the fraction and pi such that it's 1 +/- 1.0e-7. otherwise, he won't get a result until the limits are in the billions.
do.. 'if(fabs(1.0 - (PI / (i / j))) < 1.0e-7)' instead.

also, start the loops at 1, not 0.

after you fix the syntax and logical errors, you should get:
num=355, den=113
num=710, den=226
 
Sep 29, 2004
18,656
67
91
Originally posted by: Aznguy1872
Im not sure if Im doing it right at all. But my teacher just said use a double loop and try all numerators and denominators. But you see, I not good at this programming stuff. So I'm trying to get all the advice I can get. But trick question, no..

YOu have to use brute force to find an answer.

In your loop simply dividend the two numbers and see if its within your tolerance. If it is, store the numerator and denominator and return it (or print it or whatever your teacher wants)
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
I finally got it! Thank you so much. You people at anandtech are very helpful. I made the changes that itachi suggested also got rid of the semi colon after my for loops.