0

I'm programming with the C language and I need to count how many elements are in the array to use further in the program.

Most online answers have arrays with like terms (Ex: 55, 66, 77) but my array will consist of random numbers.

An example of my array would be (4, 6, 77, 450, 0, 99).

The code i'm currently using to count the size is as follows, but the code doesn't work when 0 is an element of the array.

int size = 0;

     while(*(arr+size) != '\0'){
         size++;
     }

And, I also tried to use sizeof but it also doesn't work. See the complete program below.

#define MAX 20

void display(int *arr);

 main(int argc, char *argv[]) {

     int array[MAX], count;

     /* Input MAX values from the keyboard. */
     int i;  count=0;

     while ( scanf("%d", &i) != EOF){
        *(array + count) = i; // store in array[count]
        count++;
     }

     /* Call the function and display the return value. */
      printf("Inputs: ");
      display(array);

     return 0;
 }

 /* display a int array */

 void display(int *arr) {
     int size = 0;

     int s2 = sizeof(arr)/ sizeof(arr[0]);
     printf(" -- %d -- ", s2);

     while(size <= s2){
         printf("%d ", *(arr+size));
         size++;
     }
 }

display is the function that is trying to count the elements in the array. s2 always returns value of 2, which is incorrect.

How can I count the elements of the array?

jxh
  • 69,070
  • 8
  • 110
  • 193
Daniel Ilie
  • 127
  • 7
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/201684/discussion-on-question-by-daniel-ilie-how-can-i-count-the-number-of-elements-in). – Samuel Liew Oct 31 '19 at 21:18
  • I'll repeat this as few read the chat: You simply can not get the number of elements in an array if all elements of the array can contain any value. You **MUST** pass the size in some way. Even the accepted answer kludges the array to be larger so the size can be passed. – Andrew Henle Oct 31 '19 at 21:31

1 Answers1

3

So, the only hard constraint I have seen stated so far is:

The caller of the function has to tell the function how many elements are in the array.

I'm not allowed to do that, only an integer array is to be given to the function

If this is true, then you will have to use a mechanism that is understood between the caller and the function for how to communicate the number of elements in the array.

One way to achieve this is to reserve one slot in your array to represent the size of the array. Since MAX is global to your program, your function can assume there is a number at MAX that represents the count.

int main () {
    int array[MAX+1], count;
    //... get input, calculate count ...
    array[MAX] = count;
    //... use the array ...
}

Now, when you call functions with the array, they can visit array[MAX] to find the size of the array.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • This worked, thank you. Didn't think about such an easy solution. – Daniel Ilie Oct 31 '19 at 21:12
  • 1
    As a caveat, your program lacks error checking, and in particular, you don't check the `count` does not exceed `MAX`. – jxh Oct 31 '19 at 21:14
  • Its a homework problem and the prof said just assume the count won't ever exceed the MAX – Daniel Ilie Oct 31 '19 at 21:15
  • 1
    No problem, just mentioning it for the sake of completeness. – jxh Oct 31 '19 at 21:15
  • +1 for the hideous hack. ;-) This exercise seems to have no point - it's akin to grabbing a couple of large feathers and trying to fly to the moon. – Andrew Henle Oct 31 '19 at 21:33
  • @AndrewHenle: Teachers will often apply an artificial constraint as a way to make students apply outside the box thinking. Some teachers may even accept a reasoned argument against the constraint as a valid solution. – jxh Oct 31 '19 at 21:36