Write a C++ Program to sort array of integers. Get the user input and display it in the sorted order.
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
Comments
Leave a comment