Q2B. Write a function, calculateIFYM, which takes in an argument in metres. If t
ID: 3819576 • Letter: Q
Question
Q2B. Write a function, calculateIFYM, which takes in an argument in metres. If the argument is zero or positive, calculateIFYM should return true - and “returns” the number of miles, yards, feet, and inches the argument represents. If the argument if negative, the distance in invalid: the function should return false instead, and not “return” any updated values for miles, yards, feet, and inches.
Note that:
there are 39.37 inches in a metre
there are 12 inches in a foot
there are 3 feet in a yard
there are 1760 yards in a mile
The answer must be expressed using the largest units possible, i.e. an irreducible value --for example, if the input is 2001.50 meters (78799.055 inches) you must present this as 1 mile, 428 yards, 2 feet, and 7.055 inches. Hint: calculations will be simplified if you convert meters to inches, first.
language code C++
Explanation / Answer
#include<iostream>
using namespace std;
const float metreToInches=39.37;
const float footToInches=12;
const float yardToFeet=3;
const float mileToYards=1760;
bool calculateFYM(float metres)
{
int miles,yards,feet;
float inches;
if(metres>=0)
{
double difference;
double result=(((metres*metreToInches)/footToInches)/yardToFeet)/mileToYards;
miles=result;
difference=result-miles;
result=difference*mileToYards;
yards=result;
difference=result-yards;
result=difference*yardToFeet;
feet=result;
difference=result-feet;
inches=difference*footToInches;
cout<<" There are "<<miles<<" miles "<<yards<<" yards "<<feet<<" feet "<<inches<<" inches "<<"in "<<metres<<" metres.";
return true;
}
else
return false;
}
int main()
{
bool correct;
double metres;
cout<<"Enter distance in metres: ";
cin>>metres;
correct=calculateFYM(metres);
if(!correct)
cout<<"Invalid input!";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.