for loop failure...

This is a discussion on for loop failure... within the C Programming forums, part of the General Programming Boards category; Why does my SECOND for loop (in RED) work as written , but fail when I change the values of ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    11

    for loop failure...

    Why does my SECOND for loop (in RED) work as written, but fail when I change the values of the variables? Here's the simple program (to determine prime numbers):

    Code:
    #include <stdio.h>
    #include<stdbool.h>
    
    int main (int argc, const char * argv[]) {
    
    	int p, d;
    	_Bool isPrime;
    	
    	for ( p = 2; p <= 50; ++p ) {
    		isPrime = 1;
                           for ( d=2; d<p; ++d)		
    		        if (p % d == 0)
    			isPrime = 0;
    		if (isPrime)
    			printf("%i is a prime number.\n",p);
    	}
        
        return 0;}
    The issue is this: If the SECOND for loop is written as
    Code:
    for ( d=1; d<50; ++d)
    the program exits without printing anything.

    But this for loop works perfectly:

    Code:
    for ( d=2; d<p; ++d)
    So my questions are:

    1) Why can't looping begin with d = 1 ? Why does it need to begin with d = 2 ?

    2) And why can't the limiting expression be d<50 (or some other number)? Why must it be p ?

    I'm a noob, so the issue isn't obvious. I'm posting here after hours of googling turned up nothing.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    1) If you start with d=1, then any number will be divisible by 1. Therefore your test for prime will not be valid.
    2) If you go above and beyond the tested "prime" number, you will eventually always find a number that divides into it. The number itself for example. So no number will ever be flagged as prime.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Thanks for the quick, clear response! Now it seems very clear and obvious. But without your explanation, I'd still be banging my head against the wall.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, note that you don't need to iterate until p to figure out if it is prime. If you go until sqrt(p) and you don't find any divisors then there are no prime divisors which means the number is prime.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Also, note that you don't need to iterate until p to figure out if it is prime. If you go until sqrt(p) and you don't find any divisors then there are no prime divisors which means the number is prime.
    Another quick check if you are working with ints is to check bit 0 ... bit 0 can never be 0 in a prime number (since that would mean it's and even number and thus divisible by 2)...

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Thanks, claudiu and Tater, appreciate your input -- though I was less interested in that clichéd prime numbers exercise than in why my for loop didn't seem to be working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  3. CreateWindow failure
    By TheDan in forum Windows Programming
    Replies: 6
    Last Post: 07-08-2005, 08:49 AM
  4. Am i a Failure?
    By Failure!!! in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-28-2003, 12:50 PM
  5. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 08:18 PM

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