Sunday, June 6, 2021

C Array And Sorting

 Details of the program:  Write a C program that Takes a 1-dimensional array a as input (of size 20)
Now, sort the first 5 elements in descending order and sort the last 15 elements in ascending order (do not use any pre-defined function for sorting) in a using at most 2 loops. Replace the smallest element with -1 without using any more loop and display the final array elements

#include<stdio.h>
void main()
{

int arr[20];
int i,j,t,min,index;
for(i=0;i<20;i++)
{
printf("\nValues:");
scanf("%d",&arr[i]);
 
}
for(i=0;i<5-1;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]<arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
for(i=5;i<20-1;i++)
{
for(j=i+1;j<20;j++)
{
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
if(arr[4]<arr[5])
arr[4]=-1;
else
arr[5]=-1;
printf("\nElements as follows\n");
for(i=0;i<20;i++)
printf(" %d",arr[i]);
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner