Answer to Question #346454 in C++ for Ali

Question #346454

Create an array of size 10 fill the array from the user. Then ask the user for the order of sorting ascending or descending. And sort the array using bubble sorting algorithm.

1
Expert's answer
2022-05-30T15:14:05-0400
#include <iostream>

using namespace std;

void SwapInt(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

void AscSorting(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]);
			}
		}
	}
}

void DescSorting(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]);
			}
		}
	}
}

int main()
{
	int arr[10];
	cout << "Please, enter an elements of array: " << endl;
	for (int i = 0; i < 10; i++)
	{
		cout << "Enter a value of array[" << i << "]: ";
		cin >> arr[i];
	}
	char c;
	cout << "Please, enter the order of sorting:\n"
		<< "A - ascending\n"
		<< "D - Descending\n"
		<< "Your select: ";
	cin >> c;
	if (c == 'A')
	{
		AscSorting(arr);
	}
	else if(c == 'D')
	{
		DescSorting(arr);
	}
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << " ";
	}
}

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