Is anyone able to produce a solution for this in C language? Someone else has as
ID: 3856436 • Letter: I
Question
Is anyone able to produce a solution for this in C language? Someone else has asked this question elsewhere however a correct answer was not provided:
You will need to process a (potentially large) list of integers. The goal is to either sort (into descending order) all the even number in the list, or all the odd numbers. Which numbers you need to sort is indicated by the first number of the input. The numbers of the non-indicated parity should be unaltered. E.g, if you are instructed to sort the odd numbers, then print the list with all even numbers (including 0) in their original place in the input and the odd numbers sorted into ascending order.
Input: The input consists of 3 lines: -the first line contains the word 'odd' or 'even' and indicates which numbers you need to sort -the second line contains the length of the list you will need to process -the 3rd line contains the list of integers.
Output: The processed input with either odd or even numbers sorted.
Here is a sample input:
10 (Num of dates)
January 1 01
January 1 00
February 28 99
July 17 12
September 10 12
July 1 00
June 30 90
August 25 06
May 27 08
October 1 03
1 1 00
Output:
September 10 12
July 17 12
May 27 08
August 25 06
October 1 03
January 1 01
July 1 00
January 1 00
February 28 99
June 30 90
Yes
Explanation / Answer
oddorevensort.C :
________________
#include<stdio.h>
#include<conio.h>
#include<string.h>
void sort_array();
int a[100],n,even[50],odd[50];
char parity[20]= {''};
void main()
{
int i,k=0,p=0;
clrscr();
printf("Enter even or odd:");
scanf("%s",parity);
printf("Enter length of the list:");
scanf("%d",&n);
printf("Enter elements of list: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]%2 == 0)
even[k++] = a[i];
else
odd[p++] = a[i];
if(a[i] == 0)
odd[p++] = a[i];
}
if(!strcmp(parity,"even"))
sort_array(even,k);
else
sort_array(odd,p);
getch();
}
void sort_array(int b[],int len){
int i,j,temp,s=0;
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
if(b[i] < b[j]){
temp = b[i];
b[i] = b[j];
b[j] =temp;
}
printf("Sorted List: ");
for(i=0;i<n;i++){
if(a[i]%2==0 && !strcmp(parity,"even"))
printf("%d ",b[s++]);
else if(a[i]%2==0 && !strcmp(parity,"odd"))
printf("%d ",a[i]);
else if(a[i]%2 !=0 && !strcmp(parity,"odd"))
printf("%d ", b[s++]);
else if(a[i]%2 !=0 && !strcmp(parity,"even"))
printf("%d ",a[i]);
}
}
Sample Input and Output:
______________________
Enter even or odd:even
Enter length of the list:8
Enter elements of list:
1 4 5 6 3 2 7 8
Sorted List:
1 8 5 6 3 4 7 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.