# Thread: Formula for Running word problem

1. ## Formula for Running word problem

I am attending this intro to programming 5 day course that was given for free in my community center in the neighborhood, just to get a brief knowledge of beginner programming. The guy gave us a word problem to solve and write a program with, its not a grade [considering the course was free and just for fun], but due to pride and being bad in math, I am stuck!

Here is the word problem:

An athlete runs at constant speed on a 400 meter (437.6 yards) track. Write a program
that asks the user (e.g. his coach) to input the runner speed (in kilometers per hour) and a
time interval (in seconds); the programs must calculate and display the covered distance
both in meters and yards, and also display (on a new line) the number of fully covered
laps and, in addition, the meters traveled in the last, incomplete lap (see the examples
below).
Do not use more than 2 decimal points when displaying fractional numbers.

** and here is what I have so far [I have only been in the course for one day]:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

double km, time;

cout << "Input the runner speed [km/h]:" << endl;
cin >> km;

cout << "Input the time interval [s]:" << endl;
cin >> time;

cout << "Run Distance = " << endl << endl;

cout << "Covered Laps = " << endl << endl;
return 0;
}```
anyone can help? anything!

2. You're quite lucky to have such a seminar, let a lone a free one in your neighbourhood. Good that you're taking advantage of it.

To determine the run distance, is, as usual, velocity x time or speed x time. You have the velocity/speed, in km/h. But your time is measured in seconds. So you could convert the given speed from km/h to km/s. Think about what 1km/s would be when converted to km/s (how many seconds are in one hour?). I'm sure you can do a search for converting these measurements online, to verify your answer. After you have the distance travelled, you should convert that to meters, since that is what the track is measured in. What is 1km converted to m?

Now you have the distance travelled, say, X = 1600m. Since the track is 400m long, the runner makes exactly 4 laps around. Right? Now say the distance travelled is Y=1500m. The runner makes 3 full laps around (1200m), and another 300m partial lap.

To do these calculations when the distance is a multiple of the track length (i.e. a multiple of 400), the calculation results in a whole number (or integer), like 4, 5, etc. If it is not a multiple of 400 (like 1500), then there will be a whole number/integer part (like 3) and a fractional part (like 300/400=0.75). To get this fractional part, look up examples/tutorials of the "modulus" ("%") operator. You'll find on any decent calculator a "mod" button, and 1500 mod 400 = 300.

3. Thank you! I managed to figure out how to do it. But I am stuck on one part

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

float km, time;

cout << "Input the runner speed [km/h]:" << endl;
cin >> km;

cout << "Input the time interval [s]:" << endl << endl;
cin >> time;

cout << "Run Distance = " << (km*time)/3.6 << " meters, "
<< " " << (km*time)/3.6 * 1.094 << " yards " << endl << endl;

cout << "Covered Laps = " << setprecision(1) << (time/km)/4 << " Laps, "
<< " " << (km*time)/3.6 << " meters " << endl << endl;

return 0;
}```
------------------------------
The BOLD part of the program, it wouldnt allow me to fully calculate the " << " " << (km*time)/3.6 << " meters " <<" part, when i run the program. It would give me a random number and letters.

Any idea?

4. "setprecision" by default I believe means that subsequent outputs of the numbers will print at most 1 digit (including both before and after the decimal). I think you can first set the output to "fixed", so that the "setprecision" will set the number of digits after the decimal to show. These two links, which also have examples, should explain it well enough: setprecision - C++ Reference and fixed - C++ Reference.

Next, I really doubt its printing "random numbers and letters". Its probably printing in scientific notation, where it may be showing "e" for exponent.

Next, you're calculating the distance travelled as "(km*time)/3.6", then you're determining how many full laps were covered by doing "(time/km)/4", where it should be "distance travelled/4". Id save the distance into two variables (for m, y). Then you can just print the distance in your first output, and can directly determine the number of full laps by distance/4.

As stated in the requirements, and shown in my example in my first post, you need to determine both the number of full and partial laps. Your last line will print the full laps, but not the partial laps, which I explained above.