Assignment 6
Assignment 6
Problem Definition:
Write a C program to arrange the elements of an array
into ascending and descending order.
Problem Analysis:
Input: Elements of an array
Output: Elements sorted in ascending form and
descending form.
Algorithm Development:
Flowchart:
Code:
//NAME: ARSHIA SINGH
//ROLL NO: UIT2023808
//BATCH:H1
//TITLE: To sort elements in an array.
#include<stdio.h>
int main()
{
int array[100],n,i,j,swap,a,p;
p=0;
printf("enter number of elements:");
scanf("%d",&n);
printf("enter %d integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
do{
for(i=0;i<n;i++)
{
printf("%d\n",array[i]);
}
break;
case 2:
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(array[j]<array[j+1])
{
swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;
}
}
}
for(i=0;i<n;i++)
{
printf("%d\n",array[i]);
}
break;
case 3:
printf("Exiting the code");
break;
}
}while(p!=3);
/* OUTPUT
enter number of elements:5
enter 5 integers
8 6 5 9 2
enter choice:
1)Ascending
2)Descending
3)Exit: 1
2
5
6
8
9
enter choice:
1)Ascending
2)Descending
3)Exit: 2
9
8
6
5
2
enter choice:
1)Ascending
2)Descending
3)Exit: 3
Exiting the code
*/