eratosthenos

Discussion in 'C Programming' started by Joe Smith, Apr 27, 2006.

  1. Joe Smith

    Joe Smith Guest

    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
     
    Joe Smith, Apr 27, 2006
    #1
    1. Advertising

  2. On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
    <> wrote:

    >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
     
    Barry Schwarz, Apr 30, 2006
    #2
    1. Advertising

  3. On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <>
    wrote:

    > On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
    > <> wrote:
    >


    > >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
     
    Dave Thompson, May 11, 2006
    #3
  4. Joe Smith

    Joe Smith Guest

    "Dave Thompson" <> wrote in message
    news:...
    > On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <>
    > wrote:
    >
    >> On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
    >> <> wrote:
    >>

    >
    >> >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
    -------------
    character test here ??????
     
    Joe Smith, May 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Smith

    algorithm by eratosthenos

    Joe Smith, Apr 27, 2006, in forum: C Programming
    Replies:
    21
    Views:
    735
    Joe Smith
    May 2, 2006
Loading...

Share This Page