Answer to Question #173521 in C++ for Vijay Raj

Question #173521

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-20T09:40:59-0400
#include <iostream>
 
using namespace std;
 
int main()
{
	cout << "Enter size of array: ";
	int size = 0;
	int temp = 0;
	cin >> size;
	int* a = new int[size];
	for (int i = 0; i < size; i++)
	{
		cout << "Enter " << i << " element of array: ";
		cin >> a[i];
	}
	cout << "Initial array: ";
	for (int i = 0; i < size; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl << "Sorting..." << endl;
	for (int i = 0; i < size; i++)
	{
		for (int j = 1; j < size; j++)
		{
			if (a[j - 1] > a[j])
			{
				temp = a[j - 1];
				a[j - 1] = a[j];
				a[j] = temp;
			}
		}
 
	}
	cout << "Sorted array: ";
	for (int i = 0; i < size; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

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