Skip to content

Commit e7281c7

Browse files
committed
array of structs
1 parent 1c9418d commit e7281c7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

prog.9.6.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Program to illustrate arrays of structures
2+
3+
#include <stdio.h>
4+
5+
struct time
6+
{
7+
int hour;
8+
int minutes;
9+
int seconds;
10+
};
11+
12+
int main (void)
13+
{
14+
struct time timeUpdate (struct time now);
15+
struct time testTimes[5] = {
16+
{ 11, 59, 59 }, { 12, 0, 0 }, { 1, 29, 59 },
17+
{ 23, 59, 59 }, { 19, 12, 27 }
18+
};
19+
int i;
20+
21+
for ( i = 0; i < 5; ++i ) {
22+
printf ("Time is %.2i:%.2i:%.2i", testTimes[i].hour,
23+
testTimes[i].minutes, testTimes[i].seconds);
24+
25+
testTimes[i] = timeUpdate (testTimes[i]);
26+
27+
printf (" ...one second later it's %.2i:%.2i:%.2i\n",
28+
testTimes[i].hour, testTimes[i].minutes, testTimes[i].seconds);
29+
}
30+
31+
return 0;
32+
}
33+
34+
// Function to update the time by one second
35+
36+
struct time timeUpdate (struct time now)
37+
{
38+
++now.seconds;
39+
40+
if ( now.seconds == 60 ) { // next minute
41+
now.seconds = 0;
42+
++now.minutes;
43+
44+
if ( now.minutes == 60 ) { // next hour
45+
now.minutes = 0;
46+
++now.hour;
47+
48+
if ( now.hour == 24 ) // midnight
49+
now.hour = 0;
50+
}
51+
}
52+
53+
return now;
54+
}

0 commit comments

Comments
 (0)