Sorting an array which was entered by user;
In this program I have used sorting algorithm to sort the array, and this array will be taken from user using a for loop and user only decide the size of the array ;
Here is the following source code;
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int size;
int element;
cout << "Welcome to the array generator programe" << endl
<< "Enter the size of your desierd array:\n";
cin >> size;
int arr[size];
// using for loop to take input to form a arry
for (int i = 0; i < size; i++)
{
cout << "Enter the element n.o " << i << endl;
cin >> element;
arr[i] = element;
}
// sorting the array
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
cout << "sorting and displaying your array..." << endl; // to pritify it;
// using the for loop to display the elements of the array
for (int i = 0; i < size; i++)
{
cout << arr[i] << endl;
}
return 0;
}
0 Comments