Using pointer to sort N integer numbers of an array in descending order - C

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
 
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment