Thread: Help On Pointer Sieve of E program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Help On Pointer Sieve of E program

    I made the array version of this program and it worked. So now I have to make a pointer version. But whenever I compile, it seems like the compiler is not recognizing when I dereference the pointer. I keep getting an error that says:
    invalid operands to binary %
    Here is the code:
    Code:
    #include <stdio.h>
    #define MAX 1000
    
    int sieve(int * d[])
    {
        int i;
        for (i = 0; (d + i) <= (d + MAX); i++)
        {
            if (*(d + i) % 2 == 0)
            {
                if (*(d + i) == 2)
                {
                }
                else
                {
                    *(d + i) = 0;
                }    
            }
            else if (*(d + i) % 3 == 0)
            {
                if (*(d + i) == 3)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
            else if (*(d + i) % 5 == 0)
            {
                if (*(d + i) == 5)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
            else if (*(d + i) % 7 == 0)
            {
                if (*(d + i) == 7)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
        }
        for (d + 950; d + i <= d + MAX; i++)
        {
            if (*(d + i) != 0)
            {
                printf("%d\n", *(d + i));
            }
            else
            {
            }
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your array version was probably a pointer version, using array syntax. So, what is this "array version"?

    If you want to do a "proper" "pointer version", I would recommend that you iterate using a pointer, not an index, otherwise it would be um, pointless (since the array syntax version would then be easier to read, and actually involves pointers anyway).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Here is the original array version. I am trying to make this into a pointer version. No array indexing allowed
    Code:
    #include <stdio.h>
    #define MAX 1000
    
    int sieve(int d[])
    {
        int i;
        for (i = 0; i <= MAX; i++)
        {
            if (d[i] % 2 == 0)
            {
                if (d[i] == 2)
                {
                }
                else
                {
                    d[i] = 0;
                }    
            }
            else if (d[i] % 3 == 0)
            {
                if (d[i] == 3)
                {
                }
                else
                {
                    d[i] = 0;
                }
            }
            else if (d[i] % 5 == 0)
            {
                if (d[i] == 5)
                {
                }
                else
                {
                    d[i] = 0;
                }
            }
            else if (d[i] % 7 == 0)
            {
                if (d[i] == 7)
                {
                }
                else
                {
                    d[i] = 0;
                }
            }
        }
        for (i = 950; i <= MAX; i++)
        {
            if (d[i] != 0)
            {
                printf("%d\n", d[i]);
            }
            else
            {
            }
        }
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kuletako327
    in reality you wont know how many "case"s there will be it just depends on how many inputs the user makes.
    Good. For starters, compare this:
    Code:
    int sieve(int d[])
    to what you eventually wrote:
    Code:
    int sieve(int * d[])
    Adding the * is fine, but you left the "array syntax" version of the pointer, so what you ended up with is actually equivalent to:
    Code:
    int sieve(int **d)
    Rather, it would have sufficed to change it to:
    Code:
    int sieve(int *d)
    I suspect that this will be enough to fix your problems. But if you want this assignment to be actually useful, consider this loop (my own example code snippet, not related to your code other than that d is a pointer to the first character of an array with MAX elements):
    Code:
    int i;
    for (i = 0; i < MAX; ++i)
    {
        if (d[i] % 2 == 0)
        {
            d[i] = 0;
        }
    }
    We could trivially change it to use pointer notation:
    Code:
    int i;
    for (i = 0; i < MAX; ++i)
    {
        if (*(d + i) % 2 == 0)
        {
            *(d + i) = 0;
        }
    }
    But it would be more interesting and useful if we actually used a pointer to iterate over the array:
    Code:
    int *i; /* i is now a pointer! */
    for (i = d; i < d + MAX; ++i)
    {
        if (*i % 2 == 0)
        {
            *i = 0;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Okay it works...sort of.
    Here's the whole code. It's printing out the last ten prime numbers like I want it to, but for some reason, I'm getting a random segmentation fault at the end of the program. Here is the whole code.
    Code:
    #include <stdio.h>
    #define MAX 1000
    
    int sieve(int * d);
    
    int main()
    {
        int * array_pointer;
        int s[MAX];
        int i = 0;
        array_pointer = &s[MAX];
        
        for (i = 0; i < MAX; i++)
        {
            *(array_pointer + i) = i;
        }
        sieve(array_pointer);
        return 0;
    }
    
    int sieve(int * d)
    {
        int i;
        for (i = 0; i < MAX; i++)
        {
            if (*(d + i) % 2 == 0)
            {
                if (*(d + i) == 2)
                {
                }
                else
                {
                    *(d + i) = 0;
                }    
            }
            else if (*(d + i) % 3 == 0)
            {
                if (*(d + i) == 3)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
            else if (*(d + i) % 5 == 0)
            {
                if (*(d + i) == 5)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
            else if (*(d + i) % 7 == 0)
            {
                if (*(d + i) == 7)
                {
                }
                else
                {
                    *(d + i) = 0;
                }
            }
        }
        for (i = 950; i < MAX; i++)
        {
            if (*(d + i) != 0)
            {
                printf("%d\n", *(d + i));
            }
            else
            {
            }
        }
        return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is NOT good.
    Code:
    array_pointer = &s[MAX];
    You likely want
    Code:
    array_pointer = s;
    or
    Code:
    array_pointer = &s[0];
    Tim S.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm guessing that you don't realise that your algorithm implementation is incomplete.
    For example does it include 121 in the output? 121 is not prime!

    The point is that those if statements with 2, 3, 5, and 7 are the wrong way to go about it. You need to use primes up to the square root of the max number. But you shouldn't do that by putting in yet more separate if-statements. Instead it should work out the next prime to use as it goes along. Afterall it will have already worked out what the next prime to use is well before you actually need to use it.

    Edit:
    Actually, wait a minute, why do you only output results greater than 950? There's still almost certainly going to be some wrong numbers in there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI:

    I just figured out the meaning of "Sieve of E program"; never heard of it. But, the algorithm makes sense to me; it was the way I thought to find primes many years ago; never really wrote the code out. Because by then I was passed the simple code like finding primes.

    Sieve of Eratosthenes - Wikipedia, the free encyclopedia

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Sieve Help
    By sellefrancais in forum C Programming
    Replies: 2
    Last Post: 04-03-2011, 03:04 PM
  2. Sieve of Eratosthenes
    By Eman in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2011, 05:23 AM
  3. Sieve of Eratosthenes
    By darren78 in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2010, 10:52 AM
  4. Sieve of Eratosthenes
    By jack_carver in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2010, 10:33 PM
  5. Sieve Of Erastothenes?
    By Neo1 in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2007, 02:09 AM