C programmers, help with small program [c programmers] [double loop]

admin / October 13th, 2010/ Posted in Software / No Comments »

Q: So the goal of the program I have to write: a program to require a ratio of two small (less than 1000) that PI estiamtes integers less than 0.00001% 0.

hints: great use of a .

So find I got this: # define PI 977 503 977 503 3.1415926535

int main (void) (
float i, j, r;
for (i = 0; u003c1000 i, i + +);
for (j = 0; u003c1000 i, j + +), 977 503 (
r = i / j;) 977 503 977 503 977 503 system (“PAUSE”); 977 503 return 0;) 977 503

But Im really not sure how I suppose to bring in the PI or really go beyond what I just did. Please help guys. Thanks


Best Answer: This is where header files would have helped. Header files typically contain only function and class declarations (little or no executable code) and useful constants (#defines). Source (really, code) files contain #includes for the appropriate header files and the executable code. I generally have a .h file for every .cpp file

In this case, header files for battle() and Player could separate the declarations from the definitions that need them.

Like this:

battle.h:

int battle( ….);

// No references to Player here.

Player.h:

class Player….
{
public
Player()

// No references to battle() here
}

battle.cpp:

#include battle.h
#include Player.h
//this is where the references to the Player members occur

Player.cpp:

#include battle.h
#include Player.h
// This is where the references to battle() occur

Sometimes this isn't this clean, say, where an instance of Player is an argument to the battle() function.

Hope this helps


Re:I finally got it! Thank you so much. You people at anandtech are very helpful. I made the changes that guy suggested also got rid of the semi colon after my for loops.

Re:ok, i will make the adjustments.

Re:Originally posted by: guy
Im not sure if Im doing it right at all. But my teacher just said use a 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)


Re:If you wanna really get picky, <= not < since it's "at least .00001%".

Re:'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


Re: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.


Re: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.


Re:Your for loops don't do anything productive… just count to 1000. Do your comparison to PI on the innermost .

Re:so now after that.. my values come out to be 1000, 1000 which isn't right at all. What is wrong with my code?

Re:oh ok, so should be inside the loop, thanks for the advice.

Re:Why is your if statement outside of your inner For loop? J is always going to cycle from 0 to 999 at that rate :o

Re:am I going in the right direction?

Re: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;
}


Re: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.

Re:Ah so guy, 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?

Re:You don't even need r. You just need an "if" like guy 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


Re:Im not sure if Im doing it right at all. But my teacher just said use a 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..

Re:compare r to PI inside the inner most loop, print out i and j?

is this a trick question?


Re: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 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.

Related posts


Tags: ,

Leave a Reply

Name required

Mail (will not be published) required

Website