simple math question pass by reference

How can you get a number of seconds, passing by reference, converted to hours+mins+secs...

Im thinking hours = second/3600 but to add mins do you need modulo 60? like (seonds/3600) % 60?
I need them to be hours + mins + seconds. For example, if type in 4949 seconds thats converted to 1 hr ... + _mins +_ secs...? Need help finding the formula!
Thanks again!
perhaps:

hours = seconds / 3600;
mins = (seconds - (hours * 3600)) / 60;
seconds = seconds - (minutes * 60) - (hours * 3600);
I think you're on the right track. Divide by 3600 to get number of hours, then mod that initial number by 3600, givin you the remainder. Divide that remainder to get minutes, mod it to get remaining seconds.
Not sure if that works, but it sounds solid
Just desk checked it, works
Sounds like bad logic.

You would need do take the original number and store it in a variable.
Then modulo that number by 3600 and store the result in a separate variable.
Hours is equal to the first variable minus the second variable divided by 3600.
Modulo the second variable by 60 and store the result in a third variable.
Minutes is equal to the second variable minus the third variable divided by 60.
Seconds is equal to the third variable.

EDIT: I'm sure that yours will work, but you're implicitly truncating with the assumption that the data is of type int. Mine is not making that assumption, and so it is explicit.
Last edited on
How would you write that formula wise, im a little confused resident!
would it be (seconds - (seconds % 60)) / 3600 + (seconds % 60 - ((seconds % 60) % 60)) + (seconds % 60) % 60?


Ughhh.
@ciphermagi,

Ah yea good point. I was on my phone and not really thinking. Was just adding logic onto what he already had there
Hrm...let's assume that we have a variable inTime for seconds.

mHours = (inTime - (inTime % 3600)) / 3600
mMinutes = ((inTime - (mHours * 3600)) - ((inTime - mHours) % 60)) / 60
mSeconds = ((inTime - (mHours * 3600)) - (mMinutes * 60))

EDIT: Sorry, got the math slightly wrong.
Last edited on
yea my program have an int parameter for seconds already... and three ref's parameters hours mins and secs!
Thank you so much!
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main () {
  int secs, mins, hours, seconds;
  seconds = 27926;
  secs = seconds % 60;
  mins = ((seconds - secs) / 60) % 60;
  hours = ((((seconds - secs) / 60) - mins) / 60) % 60;
  cout << hours << ":" << mins << ":" << secs << endl;
  return 0;
}
Last edited on
Topic archived. No new replies allowed.