How to display fewer items than an array

I am making a program to split even and odd integers from an array. I gave even and odd arrays of 3 elements, but when there were no 3 even / odd integers, it outputs some random numbers. Is there a way to determine what to print. Here's the code if needed:

PS I found a solution to the problem, but I have a different approach, so I just want to know that it could have been done this way, and if not, why? Thank!

#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;
}

      

+3


source to share


2 answers


Within a loop, you must use the loop counter variable i

, not fixed indices 0

, 1

and 2

.

Next, you count the number of even and the number of odd values.



Finally, you can print a list of even values ​​and a list of odd values.

int main()
{
     int nubmers[3];
     int even[3];
     int odd[3];
     int noEven = 0, noOdd = 0;

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

         if (numbers[i]%2 == 0)
             even[noEven++] = numbers[i];
         else
             odd[noOdd++] = numbers[i];
     }

    for(int i = 0; i < noEven; i++)
        printf("Even : %d\n", even[i]);
    for(int i = 0; i < noOdd; i++)
        printf("Odd : %d\n", odd[i]);

     return 0;
}  

      

+2


source


How to display fewer items than an array

Define any "Not in use" value and initialize the arrays with that.

The Not in Use value should be any value that you definitely know will not be needed / used during program run.

In your case, you can take, for example, the smallest one int

, which is defined as INT_MIN

.

#include <stdio.h> 
#include <limits.h> /* for INT_MIN */

#define NOT_IN_USE INT_MIN


int main(void)
{
  ...

  for (size_t i = 0; i < 3;: ++i)
  {
    numbers[i] = NOT_IN_USE;
    even[i] = NOT_IN_USE;
    odd[i] = NOT_IN_USE;
  }

      



Make sure the "Not in use" value is definitely not in use:

  /* core program code here */
 for(int i = 0; i < 3; i++)
 {
   printf("Enter %d element : ", i);
   scanf(" %d", &numbers[i]);
   if (NOT_IN_USE == numbers[i]) 
   {
     printf("Invalid input. Try again.\n");
     --i;
     continue;
   }

      

If later you want to check only those elements that have been used / touched only the loop through the array, skipping all elements equal to the "Not in use" value.

  /* Show results */
  for (size_t i = 0; i < 3;: ++i)
  {
    if (NOT_IN_USE != numbers[i])
    {
      printf("number[%zu] = %d\n", i, number[i]);
    }

    if (NOT_IN_USE != even[i])
    {
      printf("even[%zu] = %d\n", i, even[i]);
    }

    if (NOT_IN_USE != odd[i])
    {
      printf("odd[%zu] = %d\n", i, odd[i]);
    }
  }
}

      

+1


source







All Articles