Solved programming problem

Sorting elements of 1D array in descending order(big to small) in C.

Source code

#include<stdio.h>

int main()

{

    int array[50],i,j,n;

    int swap;

    printf("Please enter the number of element you want to use in the array:");

    scanf("%d",&n);

    printf("
PLease enter %d elements of the array:
",n);

    for(i=0; i<n; i++)

    {

        scanf("%d",&array[i]);

    }

    for(i=0; i<n-1; i++)

    {

        for(j=0; j<n-i-1; j++)

        {

            if(array[j] < array[j+1])  ///checking the number

            {

                swap=array[j];       ///replacing

                array[j]=array[j+1];

                array[j+1]=swap;

            }

        }

    }

    printf("
Your sorted array is: ");

    for(i=0; i<n; i++)

    {

        printf("%d ",array[i]);

    }

    return 0;

}

Output

Please enter the number of element you want to use in the array:5

PLease enter 5 elements of the array:

10 4 -4 9 -98

Your sorted array is: 10 9 4 -4 -98

Join the discussion

Add your comment