How to add 'nothing' into int array in C

Question

I have a task to sort an integer array into an even number array and odd number array. Then i have to show what digits has been placed in which. However, in my code not all places of the array are occupied, so in the end i receive random numbers when I want to show what the arrays odds and evens contain. Instead of random numbers I would like to have literally nothing added in their place.

I did the following:

int main()
{
   int evens[10], whole[10], odds[10], i;

   printf("Enter 10 integer(/whole) numbers\n");
   for (i = 0; i < 10; i++)
   {
      scanf("%d", &whole[i]);
      if (whole[i] % 2 == 0)
      {
      evens[i] = whole[i]; 
      else odds[i] = whole[i];
      }
   }

   printf("Your even numbers are the following:\n");
   for (i = 0; i < 10; i++)
   {
      printf("%d\n", evens[i]);
   }

   printf("Your odd numbers are the following:\n");
   for (i = 0; i < 10; i++)
   {
      printf("%d\n", odds[i]);
   }

return 0;
}

and then I get this output for having entered digits from 1 to 10:

  Your even numbers are the following:
    -1832565259
    2
    1985901418
    4
    4200864
    6
    4200958
    8
    74
    10

    Your odd numbers are the following:
    1
    4200864
    3
    6356652
    5
    1986281400
    7
    1985964450
    9
    1985901427

So how do i get an odds/even array without these random digits like 1985964450 in between? Is there a command to add literally nothing instead?


Show source
| C   | arrays   2017-01-07 19:01 4 Answers

Answers ( 4 )

  1. 2017-01-07 19:01

    It is better to have two variables that represents indexes, one you use to add odd numbers, other to add even numbers. Then you will have two arrays without redundant data :)

    int evensIndex = 0;
    int oddsIndex = 0;
    for (i=0; i<10; i++)
    {
        scanf("%d", &whole[i]);
    
        if (whole[i] %2 == 0)
        {
            evens[evensIndex] = whole[i];
            evensIndex++;
        }
        else 
        {
            odds[oddsIndex] = whole[i];
            oddsIndex++;
        }
    }
    
  2. 2017-01-07 19:01

    You should have a counter of odds and a counter of evens.

    int oddcount = 0;
    int evencount = 0;
    

    When you decide that a number is even, you use this counter to know where in the array it should go. For example:

    if (whole[i] % 2 == 0) {
        evens[evencount] = whole[i];
        evencount++;
    }
    

    Notice that evencount not only gives you the number of even numbers, but since arrays indices begin at zero, it also tells you what is the position of the next even number.

    Then you modify your for loops at the end to use the actual number of even and odd numbers that were typed. You can even check for zero and print a specific message such as No even numbers supplied.

    Also, unless you have been specifically asked to keep the input numbers in an array, you don't need whole. You can do just like so:

    int input;
    for (i=0; i<10; i++)
    {
        scanf("%d", &input);
        if (input %2 == 0)
            /* ... */
        else
            /* ... */
    }
    

    As a final remark, you should indent your code. Indentation is simply augmenting the number of spaces before the code (like I did inside the ifs above). It is very important to indent your code because it makes the structure of your code clear. For a more comprehensive discussion on this, read here: Importance of code indentation.

  3. 2017-01-07 19:01
    int main()
    {
    int evens[10], temp, odds[10], i;
    int oddIndex = 0, evenIndex = 0;
    
    printf("Enter 10 integer(/whole) numbers\n");
    for (i=0; i<10; i++)
    {
       scanf("%d", &temp);
       if(temp%2)
          odd[oddIndex++]=temp;
       else
          even[evenIdex++]=temp;
    }
    
    printf("Your even numbers are the following:\n");
    for (i=0; i<10; i++)
      printf("%d\n", evens[i]);
    
    printf("Your odd numbers are the following:\n");
    for (i=0; i<10; i++)
      printf("%d\n", odds[i]);
    
    return 0;
    }
    
  4. 2017-01-07 19:01

    Counters for odd and even will let you track the number of each.
    It is important to check the return of scanf. In this case the return can be 1 if successful, 0 if the input could not be read as an integer or EOF if End Of File is detected. In the case of 0 the input needs to be cleaned to allow further input.

    #include <stdio.h>
    
    int main( void)
    {
        int evens[10], whole[10], odds[10], i;
        int even = 0, odd = 0;
        int clean = 0;
        int result = 0;
    
        printf("Enter 10 integer(/whole) numbers\n");
        for (i=0; i<10; i++) {
            do {
                result = scanf("%d", &whole[i]);
                if ( result == EOF) {
                    fprintf ( stderr, "EOF\n");
                    return 1;
                }
                if ( result == 0) {
                    printf ( "not a number. try again\n");
                    while ( ( clean = getchar ( )) != '\n') {}
                }
            } while ( result != 1);
    
            if (whole[i] %2 == 0) {
                evens[even] = whole[i];
                even++;
            }
            else {
                odds[odd] = whole[i];
                odd++;
            }
        }
    
        printf("Your even numbers are the following:\n");
        for (i=0; i<even; i++)
            printf("%d\n", evens[i]);
    
        printf("Your odd numbers are the following:\n");
        for (i=0; i<odd; i++)
            printf("%d\n", odds[i]);
    
        return 0;
    }
    
◀ Go back