Answer to Question #291969 in C++ for Zee

Question #291969

3 samallest element in a array without using sort function and array size and element also ask by user

1
Expert's answer
2022-01-29T15:59:46-0500
#include<iostream>

using namespace std;

int main()
{
	int sz;
	cout << "Please, enter a size of array: ";
	cin >> sz;
	int* arr = new int[sz];
	cout << "Please, enter elements of array: ";
	for (int i = 0; i < sz; i++)
	{
		cin >> arr[i];
	}
	int min1 = arr[0];
	int min2 = arr[0];
	int min3 = arr[0];
	for (int i = 0; i < sz; i++)
	{
		if (min1 > arr[i])
			min1 = arr[i];
	}
	
	for (int i = 0; i < sz; i++)
	{
		if (min2 > arr[i]&& arr[i]!= min1)
			min2 = arr[i];
	}


	for (int i = 0; i < sz; i++)
	{
		if (min3 > arr[i] && arr[i] != min1&& arr[i] != min2)
			min3 = arr[i];
	}
	cout << "First 3 smallest elements are " << min1 << " " << min2 << " " << min3;

}	

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