Thread: Simple loop/array problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Simple loop/array problem

    I have the following code, which basically populates an array, and then cheks to see if one number is a multiple of another....

    I don't receive any errors, the program simply crashes as soon as it enters the "findprimes" function:
    Code:
    	// populate array (inside main)
    	for(i=0;i<=MAX;i++)
    	{
    		numbers[i] = i;
    	}
    
    // THIS FUNCTION CAUSES THE PROGRAM TO CRASH...
    void findPrimes(int numbers[])
    {
    	int a, b;
    
    	for(a=0;a<=MAX;a++)
    	{
    		for(b=0;b<=MAX;b++)
    		{
    			if(numbers[b]%numbers[a] == 0)
    				numbers[b] = 0;
    		}
    	}
    }
    Help is appreciated.
    Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    if you defined numbers as numbers[MAX], then you will try to read/write outside the array bounds.

    Either define numnbers[MAX + 1] or have the for loop go in the interval (0 <= x < MAX) instead of (0 <= x <= MAX).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    10
    I changed that part but the program continues to crash...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    	// populate array (inside main)
    	for(i=0;i<=MAX;i++)
    	{
    		numbers[i] = i;
    	}
    
    // THIS FUNCTION CAUSES THE PROGRAM TO CRASH...
    void findPrimes(int numbers[])
    {
    	int a, b;
    
    	for(a=0;a<=MAX;a++)
    	{
    		for(b=0;b<=MAX;b++)
    		{
    			if([B]numbers%numbers[a] == 0)
    				numbers[b] = 0;
    		}
    	}
    }
    Let's be more careful with division and zeros.
    The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
    Some thoughts...
    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    void findPrimes(int numbers[])
    {
        int a, b;
        for ( a = 2; a < MAX; a++ )
        {
            for ( b = 2; b < a; b++ )
            {
                if ( numbers[a] % b == 0 )
                {
                    numbers[a] = 0;
                }
            }
        }
    }
    
    int main(void)
    {
        int i, numbers[MAX];
        for ( i = 0; i < MAX; i++ )
        {
            numbers[i] = i;
        }
        findPrimes(numbers);
        for ( i = 0; i < MAX; i++ )
        {
            if(numbers[i])
            {
                printf("%d\n", numbers[i]);
            }
        }
        return 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >for(i=0;i<=MAX;i++)
    Are you sure you didn't mean
    >for(i=0;i<MAX;i++)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  3. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM