PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Minutes >> Hours+Minutes

The purpose of the program I am trying to create is to convert the inputted amount of minutes into hours and minutes.

For example:
90 minutes = 1 hour and 30 minutes

Here is where I am so far:

Code:

#include <iostream.h>
#include <stdlib.h>

int main()
{
float m, sm;
int h;

cout << "Minutes: ";
cin >> m;

h = (m \ 60)

sm =


A bit simple, but the formula is what I am stuck on, mainly for finding out the minutes. Any ideas?
Since computers are binary calculating machines, I like to convert everything to base 2 before I proceed. Once converted to base 2, finding the remainder of minutes is a trivial task. Of course, most humans function best when using base 10, so it is best to convert your answer back to base 10 as the last step. Here is how:

Code:

float a=((float)m*64.f)/60.f;
int c=((int)(a+.5f))&0x3f;
float e=((float)c*60.f)/64.f;
sm=(int)(e+.5f);


Of course, if code size, speed, accuracy, readability or sanity is an issue, a more direct manner follows:

Code:

sm=m%60;


Alex: I hope you understand that the first example was a joke. :)

OK all you obfuscation experts out there... lets see your stuff. Try to keep it within 2048 characters. Can anyone do this with the preprocessor only?

Have fun!

Cheers,

rcp
Fast, efficient, to the point:

Code:

int totaltime = 90;
int hours = totaltime / 60;
int minutes = totaltime - hours * 60;


I wouldn't do it any other way.
Code:

#include <iostream.h>
#include <stdlib.h>

int main()
{
int m, sm;
int h;

cout << "Minutes: ";
cin >> m;

h = m / 60;

sm = m % 60;

cout << "Total hours: " << h <<"\nRemaining minutes: " << sm << endl;

system("PAUSE");
return (0);
}


And Done! The only problem is that it can't calculate seconds...wait, actually that could be easily implemented by adding another sm % 60...but rather pointless.
The speed freak in me prefers Thierry's code. :)
But my evil twin prefers rcp's post - it gave me a laugh :twisted:
The whole point of the program is so a user can enter the amount of minutes, not a predetermined amount.
Alex wrote:
The whole point of the program is so a user can enter the amount of minutes, not a predetermined amount.


Huh? You already got that part working. I though you needed help with the part that converts to hours/minutes.