Write a C++ program that reads 10 integer numbers and stores them in an array Numbers, sorts the numbers using the bubble sort algorithm, and displays the array elements whenever there is an element swap. This C++ program must have three functions: 1. reading the array's ten elements, 2. displaying the array's elements, 3. The array elements are being sorted.
The Answer to the Question
is below this banner.
Can't find a solution anywhere?
NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?
Get the Answers Now!You will get a detailed answer to your question or assignment in the shortest time possible.
Here's the Solution to this Question
#include <iostream>
using namespace std;
int* ReadArray()
{
int* arr=new int[10];
cout << "Please, enter 10 elements of array: ";
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
return arr;
}
void SwapInt(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void Display(int* arr)
{
for (int k = 0; k < 10; k++)
{
cout << arr[k] << " ";
}
cout << endl;
}
void BubbleSort(int* arr)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10 - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
SwapInt(&arr[j], &arr[j + 1]);
}
}
Display(arr);
}
}
int main()
{
int* arr = ReadArray();
BubbleSort(arr);
}