-2

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);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
sadw
  • 1
  • 2
  • 1
    I don't see any code in your question that would distinguish between odd and even numbers. Generally you would do something like `if (myNumber % 2 == 0) { // number is even }` – Robert Harvey Feb 22 '22 at 15:02
  • Please don't post pictures of text but post text as text. Also post the verbatim requirement possibly translated into english. – Jabberwocky Feb 22 '22 at 15:11
  • You're right there, only thing missing is a check for even/odd before you print the numbers out. If you actually have to _remove_ them from the array as the title suggests, you've got a little more work to do. – yano Feb 22 '22 at 15:21
  • no I don't need to actually 'remove' the even numbers, I just have to hide it from the screen – sadw Feb 22 '22 at 15:35
  • In your latest update, now you must store the numbers until EOF, and then sort and print, (`realloc`, `qsort` and `printf`.) – Neil Feb 22 '22 at 15:43
  • 1
    then this is the only missing piece: https://stackoverflow.com/questions/160930/how-do-i-check-if-an-integer-is-even-or-odd/161049 – yano Feb 22 '22 at 15:48
  • there's the code but I dont even know where to insert it – sadw Feb 22 '22 at 16:07
  • Can you confirm that the requirement here is: _I have an array of numbers, and I want to print only the odd numbers in ascending order_? – Jabberwocky Feb 22 '22 at 16:23
  • 1
    If you're clueless how to insert an even/odd check before print out in the code from your question, then I'm quite dubious you wrote it. – yano Feb 22 '22 at 16:28
  • @sadw You need to do this: 1: Ask the user to enter N numbers and put them into an array. 2: Sort the numbers. 3: print only the odd numbers of the sorted. So far you have done step 1 and 2. I didn't check throughougly if your `sort` functions is correct, but at least is appears to work for my test case. Now the last step is easy: just print all odd numbers numbers from your sorted array. Write a separate function `void PrintOdd(int numbers[], int count)`. – Jabberwocky Feb 22 '22 at 16:29

4 Answers4

1

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);
}
Sathi Aiswarya
  • 2,068
  • 2
  • 11
-2

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);
}
CHAHI Saad
  • 309
  • 4
  • 15
-2

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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-2

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.

Bob__
  • 12,361
  • 3
  • 28
  • 42