Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm having trouble with this program. I'm required to use the seive of Eratosthenes to generate a list of all prime numbers between 2 and 1000. And I should use a function called sieve() to generate the list.
This is what I have of the program so far:#include <stdio.h>
int sieve();
int main(void)
{
int prime;
prime = sieve();
printf("Prime numbers 2 to 1000:\n %4d", prime);return(0);
}
int sieve()
{
int num;
int number[1000];
for(num = 0; num < 999; num++)
number[num]= num;for(num=2; num<999; num=num+2)
number[num] = 0;
for(num=3; num<999; num=num+3)
number[num] = 0;for(num=5; num<999; num=num+5)
number[num] = 0;for(num=7; num<999; num=num+7)
number[num] = 0;
if(number[num] != 0)
return();But I keep getting this error sent to me.
assign4b.c: In function `sieve':
assign4b.c:37: parse error before ')' token
And I am completely lost as to why.

Hi
The for statements should be delimited with comma's not semi-colons.
ie. for (num=7,num<999,num=num+7)

No, sorry that doesn't appear to be the problem. In fact when I change that it only causes more problems

replacing ; with , in the for loops is definitely not the solution.
From the code that you posted, it would appear that you don't have a closing bracket "}" to your function sieve().
Beyond that, after a quick look it appears that the syntax is OK, but the logic is so far off that you need to go speak with your instructor.
I'm not trying to be harsh, but the program logic is just so far off that it would take a ton of explanation and would be very hard to work thru in a forum setting.

Am I going crazy or something? I could have sworn Raxhel started a new thread with different code and a new compile-time error. If I still have some sanity left, the solution is to give sieve an int to return if number[1000]==0. In other words, your if needs an else. I'm not saying it'll make your program work, but it should make it compile.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |