Which formula we will use to solve this problem?

Pages: 12
Jan 27, 2016 at 10:01am
Problem is:-
"Many treadmills output the speed of treadmill in miles per hour on console, but most of runners think of speed in terms of a pace. A common pace is the number of minutes and seconds per mile instead of mph. So, write a program that starts with quantity in mph and coverts the quantity into minutes and seconds per mile."

So all the steps are in my mind except the formula. Which formula we will use to convert mph into miles per minutes and second?
Last edited on Jan 27, 2016 at 11:32am
Jan 27, 2016 at 11:24am
Which formula we will use to cover mph into miles per minutes and second?


That's not what it wants. It wants minutes and seconds per mile; time per mile.

Time = distance / speed

The distance we care about is one mile, so

time (in hours) = 1 / speed (in mph)

I leave it to you to work out how to convert a number of hours into minutes and seconds.

Jan 27, 2016 at 11:44am
So, what's the factor: hour -> minute / hour -> second?

Next consideration. obviously: 1 mile per hour (slow) != 1 mile per minute (fast)
So how to apply (* or /) the above factor so that the right expression is also slow?
Jan 27, 2016 at 12:58pm
Mean
First, if the treadmill is 6.5. miles per hour, divide 60 by 6.5, and we will get 9.23
second, Then we will multiply .23 with 60 to get miles per minute and second which will be 13.8

Is this is right formula?
Jan 27, 2016 at 3:37pm
Let's use better numbers. You walk 6 miles per hour. You want to know how many minutes you need for a mile.

6 miles per hour -> 0.1 miles per minute -> [Moschops:] 10 minutes per mile.
Jan 27, 2016 at 4:05pm
You start with miles / hours. Convert to seconds / mile and and the convert seconds to minutes and seconds:

1/mph = hours / mile * (3600 seconds) / hour = seconds / mile.

So 3600 / mph = seconds per mile.
Jan 27, 2016 at 4:09pm
This is what I am doing:-

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
	double mph, min, sec;

	cout << "Enter your miles per hour\n";
	cin >> mph;

	min = 60/mph;


Now at next step I am confused. For example if input for mph variable is 6.5 and dividing 60/6.5 we will 9.23 answer now for sec variable how to multiply only .23 with 60?
Mean how to separate .23 from 9 in C++?
Jan 27, 2016 at 5:40pm
Anyone to help me?
Jan 27, 2016 at 6:09pm
How about calculating seconds rather than minutes?
Then calculate minutes (and left-over seconds) from that.

Make seconds and minutes integers, because integers discard the fraction.

Integer division discards fraction as well.
Modulo operator (%) discards the whole and keeps remainder.
Jan 27, 2016 at 6:23pm
If x is miles per hour, and f(x) gives you minutes per mile, then f(x) will look like this:

f(x) = (x/60)-1

or, equivalently:
f(x) = (60/x)



Because x/60 gives you miles per minute. You must take the inverse to get minutes per mile.

This is elementary math. Googling "mph to minutes per mile" gives you the answer in the first hit.

[edit: units]
Last edited on Jan 27, 2016 at 6:26pm
Jan 27, 2016 at 6:32pm
Ok i found minute by this

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
    double  mph, minute;
    cout << "Enter miles per hour\n";
    cin >> mph;

    minute = 60 / mph;

    cout << "your answer is " << minute << endl;

}

Now, how to get seconds?
Last edited on Jan 27, 2016 at 6:33pm
Jan 27, 2016 at 6:35pm
Read Below.
Last edited on Jan 27, 2016 at 6:44pm
Jan 27, 2016 at 6:41pm
Mean how to separate .23 from 9 in C++?

int imin = int(min);
int sec = int((min - imin) * 60 + 0.5);

Note that this works for positive values only.
Jan 27, 2016 at 6:53pm
I am confused, any one provide me solution, so I can understand from it.
Jan 27, 2016 at 7:00pm
I am using book "Problem Solving With C++ (6th edition) by Walter Savitch" and facing above problem at chapter 2 but I download book in pdf with 9th edition and I am amazing to find that in 9th edition at chapter 2 this problem is not give.
I think its printing problem that this problem comes before its time mean it is very hard to solve so may be this problem is not for beginners.
What do you think why?
Last edited on Jan 27, 2016 at 7:02pm
Jan 27, 2016 at 8:46pm
I think its printing problem that this problem comes before its time mean it is very hard to solve so may be this problem is not for beginners.
What do you think why?

It's grade school math. Seems appropriate to me.
Jan 28, 2016 at 8:48am
I am confused, any one provide me solution, so I can understand from it.
If you do not understand the calculation I suggest that you break everything in parts and output the values:


1
2
3
4
5
double minute = 9.23;
int i_minute = int(minute); // = 9
double second_as_fraction = minute - i_minute; // (9.23 - 9) = .23
double second = second_as_fraction * 60; // = 13.8
double i_second = int(second + 0.5); // = 14 (+ 0.5 for rounding) 

Jan 28, 2016 at 1:00pm
Thanks #coder777

So, this is solution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
    double mph, minute, sec_as_fraction;
    int i_minute, second;

    cout << "Please enter miles per hour \"mph\".\n";
    cin >> mph;
    cout << "Ok\n";
    cout << "    \n";

    minute = 60/mph;
    i_minute = minute;
    sec_as_fraction = minute - i_minute;
    second = sec_as_fraction * 60;

    cout << "So your answer is " << i_minute << " minutes and " << second << " seconds\n";
}


Yes, I already know the procedure but I was confused on how to do these calculations in C++.
Jan 28, 2016 at 4:47pm
A solution, yes. There are more than one way (and style). Here's an another:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using std::cout;

int main()
{
    double mph {0};
    cout << "Please enter miles per hour \"mph\".\n";
    if ( (std::cin >> mph) && (0.0 < mph) ) {
        cout << "Ok\n\n";
        const int all_sec = static_cast<int>(3600.0 / mph);
        const int minute  = all_sec / 60;
        const int second  = all_sec % 60;
        cout << "Your answer is " << minute << " minutes and " << second << " seconds\n";
    }
    return 0;
}
Jan 28, 2016 at 5:43pm
What is this:-
 
intValue=static_cast<int>(dblVal);

Last edited on Jan 28, 2016 at 5:43pm
Pages: 12