Can you help me with this exercise thank you

Hello, I was trying to comprehend this exercise and I can't seem to understand what the exercise is asking, it took me along time to realise 62.14 is also 0.2614.
So I thought is it asking me (with out logic or Oop, or prototyping just simply basic understanding level)
however I kept complicating it, not intentionally.
I found the exercise example on GitHub from another individual, yet it only got even more confusing as that didn't explain what the exercise was asking only that a kilometre a litre to a gallon per mile in output text.

I thought well let's work out miles to ks, then visa versa, and predict what 38 litre car would get based on a full tank and output the distance on one journey but that wasn't the question(then I realized it could be anything, and determined a Ferrari would have a 80 litre tank and would consume alot more maybe less then 100ks) then I thought it's much better to determine a electric car then petrol because of gallon to litre conversation.

So I'm not sure what the exercise is asking?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  /*
Exercise 7 chapter 3.
Write a program that asks you to enter an automobile gasoline consumption figure
in the European style (liters per 100 kilometers) and converts to the U.S. style of
miles per gallon. Note that in addition to using different units of measurement, the
U.S.approach (distance / fuel) is the inverse of the European approach 
(fuel / dis￾tance). 
Note that 100 kilometers is 62.14 miles,and 1 gallon is 3.875 liters.Thus, 19
mpg is about 12.4 l/100 km,and 27 mpg is about 8.7 l/100 km

*/
#include <iostream>
using namespace std;
int main()
{
double  u_i;
int range = 100;
double  gpl = 0.264172;
double  lpg = 3.78541;
double  mtk = 1.609344;
double ktm = 0.6214;
cout << "Enter literes per kilometer \n";
cin >> u_i;
float m1 =  100 * lpg * ktm / u_i;
float m2 = (mtk * mtk)*u_i;
float m3 = ( mtk * u_i);
float m4 = (100 * gpl * mtk / u_i);
cout << static_cast<float>(m1)<< ": m2galon" << endl;
cout << static_cast<float>(m2)<< ": kilometres"<< endl;
cout << static_cast<float>(m3)<< ": miles"<<endl;
cout << static_cast<float>(m4)<< ": k2litre" << endl;
//float m1 = range * gpl * mtk * mtk / u_i;
   return 0;
}
Last edited on
X liters / 100 km == Y

1 / Y == 100 km / X liters == 100 km / gpl*X gallons == 100*ktm miles / gpl*X gallons
== 100*ktm/(gpl*X) miles / 1 gallon == 100*ktm*lpg/X mpg

If you are asked to:
enter an automobile gasoline consumption figure in the European style (liters per 100 kilometers) and converts to the U.S. style

then that is what you should do.
1
2
3
4
5
6
7
8
9
#include <iostream>
int main()
{
  constexpr double  lpg = 3.875;
  constexpr double ktm = 62.14;
  double  u_i {};
  std::cin >> u_i;
  std::cout << u_i << " liters / 100 km == " << lpg * ktm / u_i << " mpg\n";
}
Last edited on
So I'm not sure what the exercise is asking?

:quote. Write a program that asks you to enter an automobile gasoline consumption figure in the European style (liters per 100 kilometers) and converts to the U.S. style of miles per gallon.:endquote.
That is a long sentence, too many words. No, kidding, I assume the goal is understood, it's the way to the goal what is currently obscure.

Two words first.
Primo: always calculate with units. For example volume consists of a number and a unit, v = {v}*[v] = 8 gallon for instance. So {v} is 8 and [v] is gallon. Problem: almost all pocket calculators and programming languages offer only to enter the number, the task to get the unit right remains yours. If a calculation is somewhat obscure I recommend to do it first with a pen on paper with units, this ensures you do not add inadvertently volume and length. (In your program you could tag variable's unit as comment, may help for debugging, if necessary at all after the excellent preliminary work.)

Second primo: do unit conversions by fractions. 1 is the neutral element at multiplication, so you may multiply anything by 1 as often as you like -- it will stay the same. 5 * 1 = 5. Easy. So when you find "1 gallon is 3.875 liters" (remember a. m. v = {v}*[v]) you may write 1 gallon = 3.875 liters and transform it to 1 gallon / (3.875 liters) = 1 -- even the inverse is 1.
To convert 8.7 liters to gallons, simply calculate 8.7 liters * 1 gallon / (3.875 liters), you may cancel liters/liters = 1, compute the numbers and get = 2,25 gallons.

This said, apply it consequently (almost mechanical) to the task and it's done.
Okay I guess it was embarrassingly easy 😢 after reading that. Thank you all

It's just some other exercises offered were easier, to comprehend , population, stats, even memory sizing.

I guess it's because IM not a car person and I never really understood gallon conversation.
I never really understood gallon conversation

Bean counter disapprove ;)
Topic archived. No new replies allowed.