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