0

I am confused searching for all prime numbers between 1 and 1000. I am trying to do this and print them 13 per line. My code works for the first 5 numbers and then just ends. Here is what I have so far and my current output is 12357. Any ideas of what's wrong? Thanks in advance!

void removenonprimes(int d, int ary[]) {
  int i;
  int h = 0;

  for (i = 0; i < sizeof(ary); i++) {
    h = ary[i];
    if (h % d == 0 || h == d) {
      ary[i] = 0;
    }
  }
}

void prnt(int ary[]) {
  int i;
  int holder;
  int count = 0;

  for (i = 0; i < sizeof(ary); i++) {

    if (i > 1) {
      removenonprimes(i, ary);

      holder = ary[i];

      //case for when number is not 13th in its line
      if (holder != 0 && count != 13) {
        printf("%d", holder);
        count++;
      }
      //case for when number is 13th in its line
      else if (holder != 0 && count == 13) {
        printf("%d\n", holder);
        count = 0;
      }
    } else {
      printf("  %d", ary[i]);
    }
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Please be more specific what is wrong. – Raymond Chen Mar 27 '14 at 01:45
  • 1
    Just to tack on a bit more info gleaned from http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c: if the array were a static or "dynamic" one (i.e., declared explicitly as `int x[5]` and used by that name), then `sizeof` could be use to calculate its length using `sizeof (x) / sizeof(x[0])`. In this case, the array is passed in to the function, so its size is *not available*. – dfeuer Mar 27 '14 at 02:05

3 Answers3

2

You can't check against sizeof(ary) because sizeof(ary) returns the size of the array pointer, not the size of the array. You need to pass the size (1000) through as a parameter to your functions, or use a global constant.

void prnt(int ary[], arySize){

...

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

and call it like

prnt(nums, 1000);

You need to do a similar thing for your removenonprimes function.

The Dark
  • 8,453
  • 1
  • 16
  • 19
  • Thanks for the info! I just changed it from sizeof(ary) to 1000 and now it works. –  Mar 27 '14 at 02:02
0
  1. In your for loop you use, "i < sizeof(data);" and I do not see data declared.
  2. divisor is declare where?

I believe point number 1 will fix your issue though, you should be looking at the length of the array.

Lettergram
  • 69
  • 6
  • Doesn't `sizeof` give the size in bytes? My C is a bit rusty, but if so, that would be another problem. – dfeuer Mar 27 '14 at 01:53
  • I meant to write ary where data is right there and d for divisor since I changed a few variables.. Editing the original post now –  Mar 27 '14 at 01:58
  • You don't want to use sizeof, because it will give you the size in bytes. If you do use that, use sizeof(ary)/sizeof(int) to get the length of ary. – Lettergram Mar 27 '14 at 02:30
0

To determine the number of elements in the array :

int n = sizeof(ary) / sizeof(int);

for(i = 0; i < n; i++)
Alaeddine
  • 1,571
  • 2
  • 22
  • 46