0% found this document useful (0 votes)
7 views

Assignment 6

Uploaded by

arshiasingh9787
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 6

Uploaded by

arshiasingh9787
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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{

printf("enter choice:\n1)Ascending\n 2)Descending\n 3)Exit: ");


scanf("%d",&p);
switch(p)
{
case 1:
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 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
*/

You might also like