0

I have this code:

int main() {
    int array[5];
    int x;
    int n;
    for(x = 0; x != 5; x++) {
        scanf("%d", &n);
        if(n % 2 == 0) {
            array[x] = n;
        }
        printf("%d", sizeof(array))

I'd like to know how many variable are saved in the array.

Given that the user entered "2, 3, 5, 6, 7, 8" and it would only get "2,6,8", is there any way to get the size of that?

One way I could do this is to make another int:

int main() {
    int array[5];
    int x;
    int g = 0;
    int n;
    for(x = 0; x != 5; x++) {
        scanf("%d", &n);
        if(n % 2 == 0) {
            array[x] = n;
            g++;
        }
        printf("%d", g);

Is there any way to do this without incrementing g inside the if block?

APerson
  • 8,140
  • 8
  • 35
  • 49
HerlDerp
  • 47
  • 1
  • 1
  • 11
  • C is a low-level language. It has no concept of a variable with "no value". You declared an array of 5 ints, it allocated actual bits in memory for 5 ints, and those bits will always contain 5 ints, though since you didn't initialize the array they may be random. If you want a data structure to remember how many items were put into it, you need to build it youself, or write it in a higher-level language like Python. – Lee Daniel Crocker Nov 24 '14 at 22:47
  • its not clear what you are trying to do - you want to have up to 5 even numbers and say how many there are? - or you want an array that contains however many even numbers entered? You want the array to always be 5? – pm100 Nov 24 '14 at 22:48

2 Answers2

1

You must keep track of the count, or else you won't know what index to use for the array. Your current code doesn't work because you're leaving gaps in the array between the even numbers:

    for (x = 0; x != 5; x++) {
        scanf("%d", &n);
        if (n % 2 == 0) {
            array[x] = n;
            g++;
        }

For the input "2 3 5 6 7", you are storing the number 2 at position 0 in the array, and the number 6 at position 3. At the other positions there is random data. They aren't even zero values because you declared the array inside a function. By the way, a fixed-size array should be declared in the global scope, outside any function. That's because variables inside a function are allocated in a stack frame, which is a small and transient piece of memory. You want your arrays on the heap, which is big and long-lived.

The following code contains several improvements. It defines a constant for the length of the array, and checks the current count before saving a number. (Bad things happen if you write beyond the end of an array.) Also, this code imposes no fixed limit on the amount of data it will read. It keeps calling scanf until the end-of-file value, EOF, is returned.

#include <stdio.h>
#include <stdlib.h>

#define MAX_COUNT 1000

int even[MAX_COUNT];

int main() {
  int x,
      count = 0;                    /* Track the number of even numbers.   */
  while (scanf("%d", &x) != EOF) {  /* Get an integer from standard input. */
    if(x % 2 == 0) {                /* Is it even?                         */
      if (count == MAX_COUNT) {     /* If so, check the count first.       */
        printf("I have reached the limit of %d! I cannot store %d.",
            MAX_COUNT, x);          /* Fail gracefully if the array is     */
      } else {                      /*  full. Otherwise, we can go         */
        even[count++] = x;          /*  ahead and save the number.         */
      }
    }   
  }   
  printf("There are %d even numbers.\n", count);
  return 0;
}
Michael Laszlo
  • 12,009
  • 2
  • 29
  • 47
0

You need to keep track of this yourself.

Perhaps by using an extra variable (as you show in your second example), or by having a sentinel value that you can count up to (like how we use the null character in strings)

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173