Thread: Array Problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    16

    Array Problem

    Hello guys,i have, a problem with my array and i hope you guys can help me solve it. So i have assignment to print out number which are multiplied by prime number 2,3 and 7, and no other prime number.So here is my code:
    Code:
    #include<stdio.h>
    #define MAX 1000
    int prost(int x); // This is the function that check if number is prime
    void getpr(int s[],int m); // And this function stores in array number that are prime except 2 3 7
    void main()
    {
        int i,n,q;
        int prb[MAX];
        scanf("%d",&n);
        getpr(prb,n);    
        for(i=1;i<n;i++)
            for(q=0;q<10;q++)
               if( i%prb[q]!=0 && i%2==0 && i%3==0 && i%7==0) // Here is where i encounter a problem, 
                        printf(" %d ",i);                     //i rly dont understand why %prb[q] dont goes from 0 to 10
                                                             //and check each number if its dividable by other prime numbers
    }                                                        // and i know print out is bad, but i tried so many options and none work
    void getpr(int s[],int m)
    {
        int g=0,z=1;
        while(z<m)
        {
            z++;
            if(prost(z)==1 && z!=2 && z!=3 && z!=7)
                  s[g++]=z;
        }
    }
    int prost(int x)
    {
        int j;
        for(j=2;j<=x/2;j++)
            if(x%j==0)
                return 0;
        return 1;
    }
    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This: i%prb[q] is saying "take the value that is already stored in array prb, at spot q, and get the remainder of i divided by it."

    That's different than if you say: i % q


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Ok, but still i dont know how to do this:
    Code:
    if(i%prb[0] && i%prb[1] && i%prb[2]..... i%prb[q])
    Easy way, with loop.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There has to be a better way to do this...

    Code:
    int8_t flag = 1; 
    int32_t c = 10; /* max number of elements */
    while (c--) {
        if (!(i%prb[c])) {
            flag = 0; 
            break; 
        }
    }
    
    if (flag) {
       /* execute your code */
    }

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    God Hates Flags.
    Code:
    while (c--)
        if (i % prb[c] == 0)
            break;
    
    if (c >= 0) {
        /* whatever */
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by oogabooga View Post
    God Hates Flags.
    Then someone better tell the Westboro Baptist Church that they've made a spelling mistake.
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    #define MAX 1000
    int prost(int x); // This is the function that check if number is prime
    ...
        int prb[MAX];
        scanf("%d",&n);
        getpr(prb,n);
    ...
    void getpr(int s[],int m)
    {
        int g=0,z=1;
        while(z<m)
        {
            z++;
            if(prost(z)==1 && z!=2 && z!=3 && z!=7)
                  s[g++]=z;
        }
    }
    You never actually check to make sure n is less than MAX. Not when you enter it, and not when you run through your array getting primes. You also never actually initialize prb, so it's going to have a bunch of random junk in most spots, and only when you hit a prime just-so will it ever have something valid.
    Code:
    int prost(int x)
    {
        int j;
        for(j=2;j<=x/2;j++)
            if(x%j==0)
                return 0;
        return 1;
    }
    Why don't you return j?
    Code:
        for(i=1;i<n;i++)
            for(q=0;q<10;q++)
               if( i%prb[q]!=0 && i%2==0 && i%3==0 && i%7==0) // Here is where i encounter a problem,
                        printf(" %d ",i);                     //i rly dont understand why %prb[q] dont goes from 0 to 10
    I honestly have no idea what you are doing here. Why are you looking at only the first ten array slots in prb?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by TheBigH View Post
    Then someone better tell the Westboro Baptist Church that they've made a spelling mistake.
    All that mindless hatred, due to a simple typo.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Thank you guys!
    Quzah, what do you mean by "You also never actually initialize prb"?
    I have to say i just started working with arrays, so i realized it like this, there will be max slots, and in about xxx slots will be stored prime numbers except... , depends on n, and i did understand that the other slots xxx+ will have some junk value.So thats way i wanted to check if there is anything in array between 0 to 10.
    Well i didnt return j, because that function only checks if the number is prime and if it is, return 1. I agree with you that, it would be easier and faster with returning j, but i wanted it to be simple.

    The program still doesn't work, when i enter n , i get error.
    Code:
    for(i=1;i<n;i++)
        {
            while(c--)
                if(i % prb[c] == 0)
                    break;
            if (c >= 0)
                if(i%2==0 && i%3==0 && i%7==0)
                    printf(" %d ",i);
        }
    Last edited by Stevan; 01-31-2012 at 07:17 AM.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    I solved it finally here is the code:
    Code:
    for(i=0;i<n;i++)
        {
            b=1;
            for(c=0;c<n;c++)
            {
                if(i%prb[c]==0)
                {
                    b=0;
                    break;
                }
            }
            if(b==1)
                if( i%2==0 && i%3==0 && i%7==0)
                    printf(" %d ",i);
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  4. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 02:23 PM
  5. array problem
    By black in forum C++ Programming
    Replies: 6
    Last Post: 07-23-2002, 09:49 AM