Tuesday, March 22, 2011

Structures and Unions

Design a function update that would accept the data structure designed previously and increments time by one second and returns the new time . (If the increment results is 60 seconds, then the second member is set to zero and the minute member is incremented by one. Then, if the result is 60 minutes, the minute member is set to zero and the hour member is incremented by one. Finally when the hour becomes 24, it is set to zero.)

#include <stdio.h>
void update(struct time t);
struct time
{
int hour;
int minute;
int second;
}t;
void main()
{
clrscr();
printf("Enter hour:");
scanf("%d",&t.hour);
printf("Enter minute:");
scanf("%d",&t.minute);
printf("Enter second:");
scanf("%d",&t.second);
update(t);
getch();
}
void update(struct time t)
{
t.second=t.second+1;
if(t.second==60)
{
t.second=0;
t.minute=t.minute+1;
}
if(t.minute==60)
{
t.minute=0;
t.hour=t.hour+1;
}
if(t.hour==24)
t.hour=0;
printf("Updated time= %d:%d:%d",t.hour,t.minute,t.second);
}

OUTPUT
Enter hour: 23
Enter minute: 59
Enter second: 59
Updated time = 0:0:0

Enter hour: 23
Enter minute: 59
Enter second: 1
Updated time = 23:59:2

No comments:

Post a Comment