check every condition

This is a discussion on check every condition within the C Programming forums, part of the General Programming Boards category; i made one loop which has another loop inside it like this: Code: for(y=1;y<11;y+=1){ for(x=0;x<5000;x+=1){ if (x % y==0){ printf("%d ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    check every condition

    i made one loop which has another loop inside it like this:
    Code:
    	for(y=1;y<11;y+=1){
    		for(x=0;x<5000;x+=1){
    			if (x % y==0){
    				printf("%d \n",x);}}}
    when i run it tries the if condition indiviually b ut i am trying make it try for every "y"
    how can i do it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by alperen1994
    when i run it tries the if condition indiviually b ut i am trying make it try for every "y"
    As in you want to print all non-negative integers less than 5000 that are perfectly divisible by all the positive integers less than 11?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by alperen1994 View Post
    i am trying make it try for every "y"
    how can i do it?
    It is. Maybe you wanted this in the printf to make that clearer:
    Code:
    printf("x=%d y=%d\n",x,y);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by laserlight View Post
    As in you want to print all non-negative integers less than 5000 that are perfectly divisible by all the positive integers less than 11?
    i want the all numbers between 0-50000 that can divided with all numbers between 0-11

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by alperen1994 View Post
    i want the all numbers between 0-50000 that can divided with all numbers between 0-11
    so make outer loop for x
    and inner loop for y - and make a flag to check if you have some y tht is not devideble by...

    and print only x for which flag is not set
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by alperen1994
    i want the all numbers between 0-50000 that can divided with all numbers between 0-11
    I take that as a "yes, but the 5000 should be 50000", in which case note that the program that you posted has 5000 whereas you want 50000.

    Basically, a fix to your current code would be to make the loop that loops from x in the range [0, 50000) be the outer loop. In the inner loop, you check for divisibility, and if you find that x % y != 0, then you know that the current value of x should not be printed. If the inner loop is done and at no point is x % y != 0, then you print the current value of x and move on to the next.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Or determine a step by which to increment the number and just print the values.

    If I'm not mistaken all these numbers are 0 and 27720.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by anon
    If I'm not mistaken all these numbers are 0 and 27720.
    hmm... how did you get 27720? I reasoned that if we accounted for multiples of 8, we would cover 2 and 4. Unfortunately, I computed it as 3 * 8 * 5 * 7 * 9 = 7560, forgetting that the same reasoning that applies for 8 covering for 2 and 4 applies for 9 covering for 3. We do not need to worry about 6 and 10 since they are covered by 8, 9 and 5. As such, the sequence would be multiples of 8 * 9 * 5 * 7 = 2520, and brute force testing confirms that.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I also included 11...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by anon View Post
    I also included 11...
    The OP didn't. Whether that's a flaw in the posted code or the posted explanation will have to wait for OP.

    Edit: To be more clear, the OP said "between 0-11" and checked 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Whether 11 was supposed to be included I can't say.
    Last edited by tabstop; 03-21-2009 at 03:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. pLease check my programming~~
    By iedchan in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2007, 10:23 AM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. get and check input as while condition
    By Marksman in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2006, 09:22 PM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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