Home > database >  Hiding even numbers from display in array in C
Hiding even numbers from display in array in C

Time:02-23

Any suggestion on how to hide even numbers from user input and only printing odd numbers in ascending order? Like this output describes:

5
3
2
8
7

OUTPUT:
3
5
7
Press any key to continue . . .

I've been trying to figure it out in few hours but was unable to figure the solution :( .

#include <stdio.h>
#include <time.h>

void sort(int number[], int count)
{
    int temp, i, j, k;
    for (j = 0; j < count;   j)
    {
        for (k = j   1; k < count;   k)
        {
            if (number[j] > number[k])
            {
                temp = number[j];
                number[j] = number[k];
                number[k] = temp;
            }
        }
    }
    printf("OUTPUT:\n");
    for (i = 0; i < count;   i)
        printf("%d\n", number[i]);
}

void main()
{
    int i, number[1000];
    int count = 5;
    printf("\nType your number:");
    for (i = 0; i < count;   i)
        scanf("%d", &number[i]);
    sort(number, count);
}

CodePudding user response:

Just add 'if(number[i]%2==0)' in your program.

#include <stdio.h>
#include <time.h>

void sort(int number[], int count)
{
 int temp, i, j, k;
 for (j = 0; j < count;   j)
{
    for (k = j   1; k < count;   k)
    {
        if (number[j] > number[k])
        {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
        }
    }
}
printf("OUTPUT:\n");
for (i = 0; i < count;   i)
    if(number[i]%2!=0)
       printf("%d\n", number[i]);

}

void main()
{
int i, number[1000];
int count = 5;
printf("\nType your number:");
for (i = 0; i < count;   i)
    scanf("%d", &number[i]);
sort(number, count);

}

CodePudding user response:

try to use the following code will solve your problem :

#include <stdio.h>
#include <time.h>

void sort(int number[], int count)
{
int temp, i, j, k;
int numbs[100];
int counter = 0;
for(int h=0; h<count;   h){
    if(number[h] % 2 != 0){
      numbs[counter] = number[h];
      counter  ;
    }
}
for (j = 0; j < counter;   j)
{
    for (k = j   1; k < count;   k)
    {
        if (numbs[j] > numbs[k])
        {
            temp = numbs[j];
            numbs[j] = numbs[k];
            numbs[k] = temp;
        }
    }
}
printf("OUTPUT:\n");
for (i = 0; i < counter;   i)
    printf("%d\n", numbs[I]);
}

void main()
{
int i, number[1000];
int count = 5;
printf("\nType your number:");
for (i = 0; i < count;   i)
    scanf("%d", &number[i]);
sort(number, count);
}

CodePudding user response:

There are many ways to accomplish the task, the following might be an overkill.

Consider the interface and usage of qsort to sort an array of int in ascending order.

#include <stdlib.h>

// It needs a function that compares the values
int cmp_int(void const *lhs, void const *rhs)
{
  int const a = *(int *)lhs;
  int const b = *(int *)rhs;
  return (b < a) - (a < b); 
}

int main(void)
{
  int a[] = {42, 17, -3, 0, 8, -2, 33};
  size_t const a_size = (sizeof a) / (sizeof a[0]);
  
  qsort(a, a_size,    // The source array and the number of its elements.
        sizeof(a[0]), // The size of each element.
        cmp_int);     // The pointer to the comparator function.
  // ...
}

You can adapt the same concepts and write a function that prints only the odd elements.

#include <stdbool.h>
#include <stdio.h>

bool is_odd(int x)
{
  return (x % 2) != 0; 
}

void print_if(char const *fmt, 
              size_t n, int const *a,
              bool (*predicate)(int))
{
  for (size_t i = 0; i < n;   i)
  {
    if ( predicate(a[i]) )
      printf(fmt, a[i]);
  }
  putchar('\n');
}

int main(void)
{
  int a[] = //...
  size_t const a_size = //...

  // ...

  // Sort 'a', hopefully using qsort.

  print_if("%d ", a_size, a, is_odd);
  // ...
}

As an alternative, you could copy only the odd elements into another (suitably sized) array, sort it and print it all.

size_t copy_if(size_t n, int const *src, 
               int *dst,
               bool (*predicate)(int))
{
  size_t j = 0;
  for ( size_t i = 0; i < n;   i )
  {
    if ( predicate( src[i] ) )
    {
      dst[j] = src[i];
        j;
    }
  }
  return j; // Note that the number of elements copied is returned. Use it!
}

Live example @Compiler Explorer.

CodePudding user response:

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

As the declared array has 1000 elements then it means that the user may enter any number of values no greater than 1000. Otherwise there is no sense to declare an array with 1000 elements to enter only 5 numbers. That is you should ask the user how many integers he is going to enter.

Your sort function does not distinguish odd and even numbers. It tries to sort all elements of the array though it seems you need to sort only elements with odd values.

To sort only elements with odd values of an array you could use for example the bubble sort algorithm.

Also you should split sorting and outputting elements with odd values in two separate functions.

Here is a demonstration program that shows how the task can be performed.

#include <stdio.h>

void sort_odds( int a[], size_t n )
{
    size_t i = 0;
    
    while ( i != n && a[i] % 2 == 0 ) i  ;
    
    if ( i != n )
    {
        a  = i;
        n -= i;
        
        for ( size_t last = 0; !( n < 2 ); n = last )
        {
            last = 0;
            size_t previous = 0;
            
            for ( size_t j = 0; j < n; j   )
            {
                if ( a[j] % 2 == 1 )
                {
                    if ( a[j] < a[previous] )
                    {
                        int tmp = a[j];
                        a[j] = a[previous];
                        a[previous] = tmp;
                        
                        last = j;
                    }
                    
                    previous = j;
                }
            }
        }
    }
}

void display_odds( const int a[], size_t n )
{
    for ( size_t i = 0; i < n; i   )
    {
        if ( a[i] % 2 == 1 ) printf( "%d ", a[i] );
    }
    
    putchar( '\n' );
}

int main(void) 
{
    enum { N = 1000 };
    int number[N];

    size_t count = 0;

    printf( "Enter the number of integers you want to input (no more than %d): ", N );

    scanf( "%zu", &count );

    if ( count != 0 )
    {
        if ( N < count ) count = N;

        printf( "Enter your integers: " );

        int num;
        size_t i = 0;

        for ( ; scanf( "%d", &num ) == 1 && i < count; i   )
        {
            number[i] = num;
        }
        
        sort_odds( number, i );
        display_odds( number, i );
    }       
}

The program output might look like

Enter the number of integers you want to input (no more than 1000): 10
Enter your integers: 9 8 7 6 5 4 3 2 1 0
1 3 5 7 9 

CodePudding user response:

Any suggestion on how to remove even numbers from user input and only printing odd numbers?

Well, you can simply ignore even values at input:

void main()
{
    int i, number[1000];
    int oddcount = 0;
    int count = 5;
    printf("\nType your number:");
    for (i = 0; i < count;   i)
    {
        if (scanf("%d", &number[oddcount]) != 1) exit(1);
        if (number[oddcount] & 0x1)
        {
            // Odd number received so increment the oddcounter
              oddcount;
        }
    }

    for (i = 0; i < oddcount;   i)
    {
        printf("%d\n", number[i]);
    }
}
  • Related