0

I'm working on this program that should output 10 prime numbers. So my problem is that I dont know how to tell the program to stop as soon as 10 prime numbers have been stored in the array. I tried to do sizeof(primes)/siseof(int) == 10 , but its not working. Help me please. Thanks in advance

 int ar[100],primes[10],j,n,i,var;

 printf("Enter a prime ,\n");

 for(i=0;i<n;i++)
 {
       scanf("%d",&ar[i]);
       if (IsPrime(ar[i])) {
        primes[i] = ar[i];
        if( sizeof(primes) / sizeof(int) == 10) break;

      } else {
        printf("%d is not a prime number\n", ar[i]);
      }
 }
 printf("\narray :\n");
8
  • 1
    Increment a counter, and test if it has reached 10. Commented May 5, 2015 at 0:30
  • I tried doing if( sizeof(primes) / sizeof(int) == 10) break; but it doesnt work Commented May 5, 2015 at 0:32
  • sizeof(primes) / sizeof(int) is the size of the whole primes array, not the number that you've filled in so far. Commented May 5, 2015 at 0:33
  • 2
    primes[i] = ar[i]; Clearly that's not right. Consider if i is 80. Also, sizeof is the size of the container, not its contents. It doesn't change when the contents change. So checking sizeof is wrong too. Commented May 5, 2015 at 0:34
  • 1
    Why is your program printing the contents of ar[] at the end, not the contents of primes[]? Commented May 5, 2015 at 0:38

3 Answers 3

2

I'm going to pitch something a bit more in line with your stated intention:

#define MAX_PRIMES 10
int main(int argc, char * argv[])
{
    int inval;
    int primes[MAX_PRIMES];
    int count = 0;

    printf("Enter a prime number,\n");

    while (count < MAX_PRIMES)
    {
        scanf("%d", &inval);
        if (IsPrime(inval))
        {
            primes[count] = inval;
            count++;
        }
        else
        {
            printf("%d is not a prime number\n", inval);
        }
    }
    printf("\nThe elements of the array are:\n");

    for (int i = 0; i < MAX_PRIMES; i++)
    {
        printf(" %d", primes[i]);
    }
}

int ar[100] was replaced with int inval because there doesn't seem to be any need to store the input values.

for(i=0;i<n;i++) replaced with while (count < MAX_PRIMES) because the previous version would stop at n whether 10 primes had been found or not.

printf(" %d",ar[i]) was replaced with printf(" %d", primes[i]) because the stated desired output is 10 primes, not the input array.

Could be a stupid tyop or two in there because I haven't run it.

Sign up to request clarification or add additional context in comments.

Comments

2

Use a counter variable. Use this as the index of the primes array, because otherwise you'll write outside the bounds of the array if the user enters more than 10 numbers to try.

int ar[100],primes[10],j,n,i,var;
int primesFound = 0;
printf("Enter a prime number,\n");

for(i=0;i<n;i++)
    {
        scanf("%d",&ar[i]);
        if (IsPrime(ar[i])) {
            primes[primesFound] = ar[i];
            primesFound++;
            if( primesFound == 10) break;

        } else {
            printf("%d is not a prime number\n", ar[i]);
        }
    }
printf("\nThe elements of the array are:\n");

for(i=0;i<n;i++)
    {

        printf(" %d",ar[i]);

    }

8 Comments

This works and allow me to store 10 primer numbers :), but then after it gets the 10 prime numbers the program crushes :(
You need to use primes[primesFound], not primes[i], because i can get higher than 9.
Note that your program prints all the numbers that the user has entered, not just the prime ones, because you're printing ar, not primes.
Also, you'll print n entries of ar, even though the loop that fills in the array stops when you get to 10 primes. So the rest will be random garbage.
even if I change it to primes the program still cruches. Im uploading my entire code right now so you can run it my friend, and you will see that it crushes. Help me find what is causing the crushing please
|
1

Just add a counter

int ar[100],primes[10],j,n,i,var;

printf("Enter a prime number,\n");

int counter = 0; // here you declare a counter
for(i=0;i<n;i++)
{
    scanf("%d",&ar[i]);
    if (IsPrime(ar[i])) {
        counter++ // here you increase your counter
        primes[i] = ar[i];
        if (counter == 10) break;

    } else {
        printf("%d is not a prime number\n", ar[i]);
    }
}
printf("\nThe elements of the array are:\n");

for(i=0;i<n;i++)
{

    printf(" %d",ar[i]);

}

Now, why the if( sizeof(primes) / sizeof(int) == 10) break; doesn't work? Because your primes is an array, which is declared statically and so its size is always a constant, which is equal to 10 * sizeof(int).

3 Comments

This works and allow me to store 10 primer numbers :), but then after it gets the 10 prime numbers the program crushes :(
The program crashes because you cannot use primes[i] when i becomes bigger then 9 ... Read my and Barmar comments in your question!
the counter for "primes" should be different from the one for "ar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.