Answer to Question #276708 in C++ for shah alam

Question #276708

Create a function (order()), that arranges two numbers in ascending order

 Now ask the user to enter three numbers and by using the same function, arrange and display the entered

three numbers in increasing order


1
Expert's answer
2021-12-07T09:55:33-0500
#include <iostream>

int find_smallest(int* arr, int i);
void order(int* arr, int n)
{
	int pos, temp;
	for (int i = 0; i < n; ++i)
	{
		pos = find_smallest(arr, i);
		temp = arr[i];
		arr[i] = arr[pos];
		arr[pos] = temp;
	}
}


int find_smallest(int* arr, int i)
{
	int position = i;
	int smallest = arr[position], j;


	for (j = i + 1; j < 3; j++)
	{
		if (arr[j] < smallest)
		{
			smallest = arr[i];
			position = j;
		}
	}
	return position;
}


int main()
{
	int array[3]{};
	std::cout << "Enter three numbers" << std::endl;
	for (std::size_t i = 0; i < 3; ++i)
	{
		std::cin >> array[i];
	}
	std::cout << "Before sorting" << std::endl;
	for (std::size_t i = 0; i < 3; ++i)
	{
		std::cout << array[i] <<'\t';
	}
	std::cout << std::endl;
	order(array, 3);


	std::cout << "After sorting" << std::endl;
	for (std::size_t i = 0; i < 3; ++i)
	{
		std::cout << array[i] << '\t';
	}
	std::cout << std::endl;


	return 0;
}

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