Tuesday, 13 January 2015

Cpp Program to arrange all the elements in an array in descending order

This is a Cpp Program which arranges all the elements in an array in descending order.

Here initially first element in array and  its next element are compared array[0] and array[1] , if array[1] is less, then the values are swapped, and the process continues with 2 and 3 elements and son on until the end of array is reached.

Finally the obtained array is the sorted array in descending order.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int i,j,no,temp;

    cout << "\nEnter the no of Elements: \t";
    cin >> no;
    int arr[no];
    for(i=0;i<no;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> arr[i];
    }

    for(i=0;i<no;i++)
    {
        for(j=i;j<no;j++)
        {
            if(arr[i] < arr[j])
     {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }

    cout << ("\nSorted array: \n");
    for(i=0;i<no;i++)
    {
    cout << "\t" << arr[i];
    }
    return 0;
}

OUTPUT : 
Cpp - Descending Order Array

No comments:

Post a Comment