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!
#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";
}
Comments
Leave a comment