Write a C++ program that will accept an input for the array size. Store the user input and sort array elements in ascending order.
Sample Output:
Enter array size( Max:50 ) :: 8
Enter array elements ::
Enter arr[0] Element :: 4
Enter arr[1] Element :: 1
Enter arr[2] Element :: 5
Enter arr[3] Element :: 8
Enter arr[4] Element :: 0
Enter arr[5] Element :: 9
Enter arr[6] Element :: 4
Enter arr[7] Element :: 1
Stored Data Before Sorting In Array ::
4 1 5 8 0 9 4 1
Stored Data After Sorting In Array ::
0 1 1 4 4 5 8 9
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arrSz;
cout << "Please, enter the array size: ";
cin >> arrSz;
if (arrSz > 50)
cout << "Array size can be only less than 50";
else
{
int *pa = new int[arrSz];
cout << "Enter array elements ::\n";
for (int i = 0; i < arrSz; i++)
{
cout << "Enter arr[" << i << "] Element :: ";
cin >> pa[i];
}
cout << "\nStored Data Before Sorting In Array :: ";
for (int i = 0; i < arrSz; i++)
{
cout << pa[i]<<" ";
}
sort(pa, pa + arrSz);
cout << "\nStored Data After Sorting In Array :: ";
for (int i = 0; i < arrSz; i++)
{
cout << pa[i] << " ";
}
}
}
Comments
Leave a comment