Q. Write a program using pointer to sort N integer numbers of an array in descending order.
#BIT_FirstSem_2016_1b_Solution
==> Code goes like this:
/* Decending sort using pointer */
#include<stdio.h>
int main()
{
int a[10],i,j,n,temp;
printf("How many numbers: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for ( i = 0; i < n; i++)
{
scanf("%d",a+i);
}
//Sorting in Descending order
for ( i = 0; i < n-1; i++)
{
for ( j = i+1; j < n; j++)
{
if(*(a+i)<*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
//Printing after sorting in descending order
for ( i = 0; i < n; i++)
{
printf("%d \t",*(a+i));
}
return 0;
}
Output:
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Using pointer to sort N integer numbers of an array in descending order - C
2:04 PM
Ascending & Descending Order in C
,
OldQuestionSolution2016C
,
pointer in c
,
Sorting in C
Edit
0 comments:
Post a Comment