I am making a code that will:
- print an array of random numbers
- order it to an ascending order
- find the odd numbers from that array
- arrange the odd numbers to a descending order.
I've done the first three but I can't arrange the odd numbers to a descending order. The program sorts the random numbers to a descending order instead of just the odd numbers. Please help me. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int size,i,j,temp;
int a[50];
printf("Enter array size| ");
scanf("%i",&size);
srand(time(0));
printf("\nThe Random Numbers| \n");
for(i=0;i<size;i++){
a[i] = rand()%100;
printf("%i, ",a[i]);
}
for (i=0;i<size;++i)
{
for (j=i+1;j<size;++j)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nThe Ascending Order of Random Numbers| \n");
for (i=0;i<size;++i)
printf("%d, ", a[i]);
printf("\n\nThe Odd Numbers| \n");
for(i=0;i<size;i++){
if(a[i]%2!=0){
printf("%i, ",a[i]);
}
}
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nThe Descending Order of Odds| \n");
for(i=0;i<size;i++)
printf("%d ",a[i]);
return 0;
}