Distance per Tank of Gas

IMA
Creative Commons License photo credit: billaday

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (2) was titled “Introduction to C++”.

2.11: A car with a 20-gallon gas tank averages 21.5 miles per gallon when driven in town and 26.8 miles per gallon when driven on the highway. Write a program that calculates and displays the distance the car can travel on one tank of gas when driven in town and when driven on the highway.

Hint: The following formula can be used to calculate the distance:

Distance = Number of Gallons X Average Miles per Gallon.

My solution (using hard-coded values as per instructions):

/* Chapter 2 Challenge 11
Written by Kirk Hingsberger
September 11, 2007
Distance per Tank of Gas Challenge
*/

#include
using namespace std;

int main()
{
float gallons = 20;
float mpgTown = 21.5;
float mpgHwy = 26.8;
float distanceTown = gallons * mpgTown;
float distanceHwy = gallons * mpgHwy;

cout << "A car with a " << gallons << "-gallon gas tank earning ";
cout << mpgTown << " miles per gallon in town can travel ";
cout << distanceTown << " miles in town on a full tank." << endl;
cout << "A car with a " << gallons << "-gallon gas tank earning ";
cout << mpgHwy << " miles per gallon on Highway can travel ";
cout << distanceHwy << " Highway miles on a full tank." << endl;

return 0;
}

As with many other beginning C++ programs, we were instructed to not sweat the fact that our figures computed in scientific notation or with many decimal places. We would learn those controls later.

Here is the capture script I turned in for grading:

Script started on Thu 13 Sep 2007 10:17:23 AM MDT
$ cat chap2chlg11.cc
/* Chapter 2 Challenge 11
Written by Kirk Hingsberger
September 11, 2007
Distance per Tank of Gas Challenge
*/

#include
using namespace std;

int main()
{
float gallons = 20;
float mpgTown = 21.5;
float mpgHwy = 26.8;
float distanceTown = gallons * mpgTown;
float distanceHwy = gallons * mpgHwy;

cout << “A car with a ” << gallons << “-gallon gas tank earning “;
cout << mpgTown << ” miles per gallon in town can travel “;
cout << distanceTown << ” miles in town on a full tank.” << endl;
cout << “A car with a ” << gallons << “-gallon gas tank earning “;
cout << mpgHwy << ” miles per gallon on Highway can travel “;
cout << distanceHwy << ” Highway miles on a full tank.” << endl;

return 0;
}
$ g++ -o chap2chlg11 chap2chlg11.cc -s
$ chap2chlg11
A car with a 20-gallon gas tank earning 21.5 miles per gallon in town can travel 430 miles in town on a full tank.
A car with a 20-gallon gas tank earning 26.8 miles per gallon on Highway can travel 536 Highway miles on a full tank.
$ exit
exit

Script done on Thu 13 Sep 2007 10:17:52 AM MDT

  • Share/Bookmark




4 Responses to “Distance per Tank of Gas”

  1. Wouldn’t that be ‘double’ instead of ‘float’?
    becasue I get a…
    “Warning C4305: ‘initializing’ : truncation from ‘double’ to ‘float’”
    message in the warning list.

    It will compile/run with ‘float’, but that error doesn’t show up when ‘double’ is used.

  2. No, mine compiled fine as float. I used the gcc compiler, maybe you used another compiler and it was pickier?

  3. I used the compiler which came with MS Visual Studio 2008 Pro.
    It compiled alright with float with no problem, but double is commonly used with floating numbers as well since they both deal with decimal numbers. I asked my teacher about it, and she said that double would work good in this situation.

    Although it works right with float as well, I thank you, becasue I had a hell of a time trying to figure out what mathematical equation to use, lol.
    Math isn’t my strong point, so I didn’t know how to word it.

    Don’t worry, I didn’t copy the stuff word-for-word. the coding looks similar but has different variable names, comments, and the output is different as well in the wording.

    It’s nice to see some1 out there is using the same book as me ^.^

  4. There you go, yeah different compiler behaviors. I made this program almost two years ago so you’re probably much more up to speed on the nuances between doubles and floats.

    I don’t care if you or anyone copies the code word-for-word, but what’s important is that you care. It’s too easy to cheat and copy, but my classmates who did that didn’t make it far into our program because they weren’t really learning how to program. Nice to see that you recognize the difference.

    I’ve been meaning to get back to posting these old programs, I took the summer off. It’s nice to see they are useful for other students to validate the programs they write. Glad to help!

Leave a Reply