eratosthenos

J

Joe Smith

It is nothing short of embarrassing to feel the need to ask for help on
this. I can't see how I would make the main control for this. What I want
is a for loop and a test condition. And while I know, from things I
pondered 2 decades ago, that a fella can write code without a goto, I'm
stuck.

/* sieve1.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[N+1], m, sum;

/* initialize to 0 */

for (i = 0; i <= N; ++ i) A = 0;



/* output */
printf("Primes less than N are:\n");
for (i = 2; i <= N; ++ i)
{
if (A == 0)
printf("%d
 
B

Barry Schwarz

It is nothing short of embarrassing to feel the need to ask for help on
this. I can't see how I would make the main control for this. What I want
is a for loop and a test condition. And while I know, from things I
pondered 2 decades ago, that a fella can write code without a goto, I'm
stuck.

/* sieve1.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[N+1], m, sum;

If you change it to A[N+1]=0 you can eliminate the initialization loop
below.
/* initialize to 0 */

for (i = 0; i <= N; ++ i) A = 0;

For each i that is not prime, you want to set A to a non-zero
value. One way is

for (i = 2; i <= N; i++)
if (A == 0)
for (j = i+i; j <= N; j += i)
A[j] = 1;
/* output */
printf("Primes less than N are:\n");
for (i = 2; i <= N; ++ i)
{
if (A == 0)
printf("%d

It looks like the tail end of your program fell into the bit bucket.


Remove del for email
 
D

Dave Thompson

On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
int i, A[N+1], m, sum;

If you change it to A[N+1]=0 you can eliminate the initialization loop
below.
Must use braces for an array: ... A[N+1] = {0}


- David.Thompson1 at worldnet.att.net
 
J

Joe Smith

Dave Thompson said:
On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
int i, A[N+1], m, sum;

If you change it to A[N+1]=0 you can eliminate the initialization loop
below.
Must use braces for an array: ... A[N+1] = {0}

Thanks. Down yonder I think I have this algorithm correct using static and
dynamic mem allocation. joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top