C++ Program to Sort Elements





This program reads a set of numbers from the standard input device and sort them in ascending order.

Nested for loop is used in this program. This is the simplest way of sorting the numbers that have been inserted through a standard input device.

 

#include<iostream.h>

#include<conio.h>

Void main ()

{

Int a [100], i, j, n, temp;

cout<<”\n How many numbers?”;

Cin>>n;

cout<<”\n Enter the elements:”;

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

{

Cin>>a[i];

}

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

{

For (j=i+1; j<n; i++)

{

If (a[i]> a[j])

{

temp=a[i];

a[i] =a[j];

a[j] =temp;

}

}

}

Difference=a [n-1]-a [0];

Cout<<”\n the largest element is :”<< a [n-1];

Cout<<”\n the smallest number is :”<< a [0];

Cout<<”\n Difference between them is :”<< difference;

getch ();

}



Related posts:

  1. C++ Program To Display Result Stored In An Array This program takes ‘n’ number as input , stores it...
  2. C++ Program using Stadd() and Stshow() This program is designed in-order to take input  of 5...
  3. C++ Program Using getdata() and display() The program uses member function getdata() and display() in-order to...
  4. C++ Program Using Switch Statement This program is used to check whether the entered character...
  5. C++ Program to Find Sum and Average Using Class Class is the expanded concept of data structure, instead of...