INTERVIEW C QUESTION (ZOHO)
******************************************************************************QUESTION
Write a program to sort the elements in odd positions in descending order and elements in ascending orderEg 1: Input: 13,2 4,15,12,10,5
Output: 13,2,12,10,5,15,4
Eg 2: Input: 1,2,3,4,5,6,7,8,9
Output: 9,2,7,4,5,6,3,8,1
*******************************************************************************/
PROGRAM
#include <stdio.h>#include<string.h>
int main()
{
int a[100],b=0,i,j,temp;
while((scanf("%d ",&a[b]))>0)
{
b++;
}
for(i=0;i<b;i=i+2)
{
for(j=i+2;j<b;j=j+2)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=1;i<b;i=i+2)
{
for(j=i+2;j<b;j=j+2)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<b;i++)
{
printf("%d ",a[i]);
}
}
Comments
Post a Comment