16. Baseball - solution

In this task the throws of the competitors are read in yards, feet and inches, and then the longest throw is found and shown in metres and centimetres. 
Note: One inch is 2.54 centimetres. One foot has 12 inches, and one yard has 3 feet.
To solve this task, we need to read, in a loop, the values of the current throw given in yards (yard), feet (foot) and inches (inches), and inside the current iteration convert everything to inches. Also, during the loop we find the maximum of the values converted to inches, which is actually the longest throw.
When the maximum length of the throw in inches is found, it should be converted to metres and centimetres. For this we can use the modulo (%) and division (/) operators, where  % gives the remainder when the number is divided by 100 (centimetres), while / gives the integer result (metres), as explained in tasks 1 and 2 on the page: Operators - examples (SR)

Example input and desired output

Input:
18 2 7
18 1 7
14 0 9

Output:
17 25
The program assumes input for only three throws, but the number of throws can easily be adjusted by changing the number of iterations of the for loop.
Note:  The output in the format "metres centimetres" is  without any additional words.

Solution in C++

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int yard,foot,inches;
int throwInches,maxInches=0; //current throw in inches, maximum throw initialized to zero
int m, cm; //metres, centimetres
for(int i=0; i<3; i++)
    {
cin >> yard >> foot >> inches;
        throwInches=3*12*yard+12*foot+inches; //converts the length of the throw completely to inches
if(throwInches > maxInches)
        {
            maxInches=throwInches; // Updates the maximum length
        }
    }
    m=(int)(round(maxInches*2.54)); //Converting the maximum length from inches to centimetres and rounding to the nearest integer.
    cm=m%100; //remainder in cm
    m=m/100; //maximum throw in metres
cout << m << " " << cm << endl;
return 0;
}

Previous
​|<Municipal competitions (SR)