Q:

C program to sort a one dimensional array in descending order

0

C program to sort a one dimensional array in descending order

Given an array with N integer elements and we have sort them in descending order.

Example:

    Input array elements:
    10, 10, 20, 30, 10

    Output:
    Sorted array elements:
    30, 20, 10, 10, 10

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

/* C program to sort an one dimensional array 
in descending order.*/

#include <stdio.h>
#define MAX 100

int main()
{
    int arr[MAX], n, i, j;
    int temp;

    printf("Enter total number of elements: ");
    scanf("%d", &n);

    //read array elements
    printf("Enter array elements:\n");
    for (i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    //sort array
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (arr[i] < arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    printf("\nArray elements after sorting:\n");
    for (i = 0; i < n; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

Output:

    Enter total number of elements: 5
    Enter array elements:
    Enter element 1: 100
    Enter element 2: 999
    Enter element 3: 200
    Enter element 4: 800
    Enter element 5: 300

    Array elements after sorting:
    999
    800
    300
    200
    100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to delete given element from one dimensi... >>
<< C program to sort a one dimensional array in ascen...