Computing.Net > Forums > Programming > Sieve of Eratosthenes C prgm(again)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sieve of Eratosthenes C prgm(again)

Reply to Message Icon

Name: Raxhel
Date: March 29, 2004 at 14:07:41 Pacific
OS: linux
CPU/Ram: pent4
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: March 29, 2004 at 14:30:31 Pacific
Reply:

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


0

Response Number 2
Name: Raxhel
Date: March 29, 2004 at 15:26:07 Pacific
Reply:

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


0

Response Number 3
Name: Don Arnett
Date: March 29, 2004 at 15:36:53 Pacific
Reply:

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.


0

Response Number 4
Name: Jake
Date: March 29, 2004 at 17:56:47 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Sieve of Eratosthenes C prgm(again)

Sieve of Eratosthenes C prgm www.computing.net/answers/programming/sieve-of-eratosthenes-c-prgm/9965.html

The sieve of Eratosthenes (C prgrm) www.computing.net/answers/programming/the-sieve-of-eratosthenes-c-prgrm/9257.html

changing the color of Turbo c++ www.computing.net/answers/programming/changing-the-color-of-turbo-c-/13874.html