Friday, 6 May 2016

C++ Program of Array to Sort 10 Integers in Descending Order

Dynamic Array:
#include <iostream>
using namespace std;
const int N=10;

int main()
{
    int a[N],i,nb,tmp;

    for(i=0;i<N;i++)
    {
        cout << "Please enter an integer: ";
        cin >> a[i];
    }
    do {
        nb=0;
        for (i=0;i<N-1;i++)
            if (a[i]>a[i+1])
            {
                tmp=a[i];a[i]=a[i+1];a[i+1]=tmp;
                nb++;
            }
    } while(nb!=0);

    cout << "The sorted array:" << endl;
    for (i=0;i<N;i++)
        cout << "a[" << i << "] = " << a[i] << endl;

    return 0;
}
Share:

0 comments:

Post a Comment