How to display less elements than there is in an array

Viewed 108

I am making a program to separate even and odd integers from an array. I gave the even and odd arrays 3 elements but when there aren't 3 even/odd integers it prints out some random numbers. Is there any way to define what to print out. Here is the code if needed:

P.S. I found the solution to the problem but it has different approach,so i just want to know i it could have been done this way and if not, why? Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>

int main()
{
     int nubmers[3];
     int even[3];
     int odd[3];

     for(int i = 0; i < 3; i++)
     {
         printf("Enter %d element : ", i);
         scanf(" %d", &numbers[i]);

         if(numbers[0]%2 == 0)
         {
             even[0] = numbers[0];
         }
         else
         {
             odd[0] = numbers[0];
         }

         if(nubmers[1]%2 == 0)
         {
             even[1] = numbers[1];
         }
         else
         {
             odd[1] = numbers[1];
         }

         if(numbers[2]%2 == 0)
         {
             even[2] = numbers[2];
         }
         else
         {
             odd[2] = numbers[2];
         }
     }

     printf("Even : %d\n", even[i]);
     printf("Odd : %d\n", odd[i]);

     return 0;
}
2 Answers
Related