Article 4QSG8 A question about time...

A question about time...

by
jsbjsb001
from LinuxQuestions.org on (#4QSG8)
Now I know what ya's are thinking... how's "time" a programming question? Well, good question (pun intended)... Basically I've been practicing some structures and loops and I thought, "well what better way than to create a clock". So I did, but before I get to my question, a little background...

Basically I wrote a little program that has a time structure for the hours, minutes, and seconds, with some loops to increment them. My first problem was that since time never stops, then setting a condition on the first loop (that houses my nested loops) would mean either, the loop would never run, or it would stop rather than continue to increment the time. So I didn't set a condition in the for loop's initialization field where you have the initial expression, condition, and the loop expression. But because it would therefore keep going forever, I wrote an if statement in the body of the first loop so it stops after 3 hours.

The second problem I had was that I DID have to set a condition for the two nested loops so when the seconds reach 60, the minute is incremented, and then the same so the hour gets incremented. But unless you reset the structure member for minutes and seconds back to zero once they reach 60, then the minutes and hours won't get incremented - so I did just that, and that solved that problem. Which brings me to my question and third problem...

Because the computer is fast, and doesn't take three hours to increment my loops, well one or two seconds (if even that) doesn't equal three hours. In other words; it doesn't take three hours to increment the time to three hours. I imagine the solution in regards to giving the computer the concept of "a second" involves some sort of math calculation, but I have no idea what that would even look like.

Here's my code:

Code:#include <stdio.h>

int main(void) {

int i;

struct time {
int hour, minute, second;
};

struct time now;

now.hour = 0;
now.minute = 0;
now.second = 0;

for ( i = 0; ; ++i ) {
++now.second;
if ( now.hour == 3) {
break;
}
while ( now.second == 61 ) {
now.second = 0;
++now.minute;
}
while ( now.minute == 61 ) {
now.minute = 0;
++now.hour;
}
printf("Time is now: %i:%i:%i\n", now.hour, now.minute, now.second);
}

printf("\nfor loop iterations: %i\n", i);

return 0;
}Here's how it works:

Code:...
Time is now: 2:60:39
Time is now: 2:60:40
Time is now: 2:60:41
Time is now: 2:60:42
Time is now: 2:60:43
Time is now: 2:60:44
Time is now: 2:60:45
Time is now: 2:60:46
Time is now: 2:60:47
Time is now: 2:60:48
Time is now: 2:60:49
Time is now: 2:60:50
Time is now: 2:60:51
Time is now: 2:60:52
Time is now: 2:60:53
Time is now: 2:60:54
Time is now: 2:60:55
Time is now: 2:60:56
Time is now: 2:60:57
Time is now: 2:60:58
Time is now: 2:60:59
Time is now: 2:60:60
Time is now: 3:0:0

for loop iterations: 11163Now clearly that took maybe not even one second. How do I give the code the concept of an actual second (that would be a "second" to you or me, eg. 1..2..3..etc, rather than 123456789,etc)?

Jameslatest?d=yIl2AUoC8zA latest?i=Bm47Vf-f_Lg:Ga6rjIa_Hwc:F7zBnMy latest?i=Bm47Vf-f_Lg:Ga6rjIa_Hwc:V_sGLiP latest?d=qj6IDK7rITs latest?i=Bm47Vf-f_Lg:Ga6rjIa_Hwc:gIN9vFwBm47Vf-f_Lg
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments