Solutions to Project Section 2


I'm very sorry that I couldn't put up the solutions earlier. Anyway, the solutions to the ascending and descending order programs are below :


First Program : 

//To arrange a given set of numbers in ascending order and display the result
#include <iostream.h>
int main ( )
{
int test[10];
int size, i , j;
int temp;
cout<<"How many numbers do you want to compare : ";
cin>>size;
cout<<"Enter the numbers you want to check : ";
for(i = 0; i<size; i ++)
{
cin>>test[ i ];
}
for(i = 0; i<size; i++)
{
    for (j = 0; j<size; j++)
        {
                    if(test[ i ]>test[ j ])
                                {
                                    temp = test[ i ];
                                    test[ i ] = test[ j ];
                                    test[ j ] = temp;
                                  }
            }

cout<<"The numbers in ascending order are : ";
for (i = 0; i<size; i ++)
{
cout<<endl<<test[ i ];
}
return 0;
}


Second Program:
// To arrange the elements in descending order, just make one small change.
Instead of test [ i ]>test [ j ], type test [ i ] < test [ j ]


Go to the next section on : Character Arrays

or return back to Contents.


Copyright © 2002 Sethu Subramanian All rights reserved.