Answer to Question #327295 in C++ for ness

Question #327295

Where's the Biggest One?

by CodeChum Admin


We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate the position of the largest among 5 numbers. There are only 4 possible cases for this mission:


- if the largest digit is the first digit, print "Leftmost"


- if the largest digit is the third digit, print "Middle"


- if the largest digit is the last digit, print "Rightmost"


- if none of the above is correct, print "Unknown"




Now, show me how far you've understood your lessons!


1
Expert's answer
2022-04-11T12:13:32-0400
#include<iostream>

using namespace std;

int main()
{
	const int N=5;
	int arr[N];
	cout << "Please, enter 5 numbers: ";
	for (int i = 0; i < N; i++)
	{
		cin >> arr[i];
	}
	int largest = arr[0];
	int pos = 0;
	for (int i = 0; i < N; i++)
	{
		if (arr[i] > largest)
		{
			largest = arr[i];
			pos = i;
		}
	}
	if (pos == 0)
		cout << "Leftmost";
	else if(pos==2)
		cout << "Middle";
	else if (pos == 4)
		cout << "Rightmost";
	else
		cout << "Unknown";
}

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