Answer to Question #305419 in C++ for sadwe

Question #305419

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.

1
Expert's answer
2022-03-03T09:59:13-0500
#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);
}

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