Answer to Question #172500 in C++ for Boopathi

Question #172500

Write a C++ Program to sort array of integers. Get the user input and display it in the sorted order.


1
Expert's answer
2021-03-18T11:14:21-0400

Using standard sort() function:

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int main()
{
   const int Length = 100;
   float arr[Length], x;
   int i, cnt;
   cout << "Enter up to 100 numbers \n"
        << "(Quit with a letter):" << endl;
   for( i = 0; i < Length  &&  cin >> x; ++i)
      arr[i] = x;
   cnt = i;
   cout << "The given numbers:\n" << endl;
   for( i = 0; i < cnt; ++i)
      cout << " " << arr[i];
   cout << endl;
 
   sort(arr, arr + cnt);
   
   cout << "Sorted array:\n" << endl;
   for( i = 0; i < cnt; ++i)
      cout << " " << arr[i];
   cout << endl;
   return 0;
}



The output looks like following:

Enter up to 100 numbers
(Quit with a letter):
1 5 8 2 -1 q
The given numbers:

 1 5 8 2 -1
Sorted array:

 -1 1 2 5 8

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS