Showing posts with label Sort Array in Descending Order. Show all posts
Showing posts with label Sort Array in Descending Order. Show all posts

Saturday, 2 August 2014

Write a Program in C++ which sort array in descending order

Structure of the Problem Requirements 


In this problem we will learn how to sort array in descending order . In this solution for more clarification we use three for loops so everyone can easily understand the function of descending sorting of array.The Program will take 5 integers from user and then show them to user as user enter with the message "your number before descending order" after this bubble sorting logic apply to that array and sort them in descending order.

Source Code 






#include<iostream>
using namespace std;
int main ()
{
int num [5];
int temp =0;
for (int i =0; i<5;i++)
{
cout<<" Enter Your Number : ";
cin>>num[i];
}
cout<<endl;
cout<<"  Your Number Before Sorting in Descending order \n  ";
for (int i =0;i<5;i++)
{
 cout<<endl<<num[i];
}
cout<<" \n Your Number After Sorting in Descending order ";
for (int i =0;i<5;i++)
{
for (int k =0;k<5-1;k++)
{
if (num[k+1] > num[k])
{
temp = num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
for (int m =0;m <5;m ++)
{
cout<<endl<<num[m];
}
}

Output of the Program


C++ Array Programming
Sorted Array in Descending Order