The requirement is to take in 10 integers from user, and sort them according to even, odd and negative numbers.
Challenges
1. The numbers are arbitrary, you never know which number types user will put in.
2. the number of even, odd and negative numbers are dynamic, however you do know that maximum number of integers is ten.
3. You will need to print out even, odd and negative numbers of different length because there will be a mixture of odd, even and negative numbers, negative numbers might not be entered and so on…
Here’s the sample code in c:
#include <stdio.h> int main() { int num,evenList[10],oddList[10],negativeList[10], ei=0,oi=0,ni=0,i; printf("Enter 10 integers:\n"); for(i=0;i<10;i++) { scanf("%d",&num); if(num<0) { negativeList[ni]=num; ni++; } else if(num%2==0) { evenList[ei]=num; ei++; } else { oddList[oi]=num; oi++; } } printf("Elements in even array\n"); for(i=0;i<ei;i++) { printf("%d\t",evenList[i]); } printf("\n"); printf("Elements in odd array\n"); for(i=0;i<oi;i++) { printf("%d\t",oddList[i]); } printf("\n"); printf("Elements in negative array\n"); for(i=0;i<ni;i++) { printf("%d\t",negativeList[i]); } printf("\n"); return 0; }
Below is the test sample:
Enter 10 integers:
-10 -10 -10
20
30
40
50
60
87
89
Elements in even array
20 30 40 50 60
Elements in odd array
87 89
Elements in negative array
-10 -10 -10