Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program that updates the time by one second. Use a struct with three

ID: 3564256 • Letter: 1

Question

1. Write a program that updates the time by one second. Use a struct with three integer members representing hours, minutes and seconds. The program should accept military time expressed using a twenty four-hour clock. Output the updated time as being A.M. or P.M.

2. Write a program, which accepts as input the date and outputs the date one-week in the future. Assume that the date is represented as three integers in the form mm dd yyyy, where mm is the month, dd the day in month and yyyy the year. A year is a leap year if the following conditions hold; the year is divisible by 4 and not divisible by 100, or the year is divisible by 400.

Explanation / Answer

1.
#include <stdio.h>

   struct time {
   int hour, minutes, seconds;
       char ampm;
   };

   void time_update( struct time );  

   /* function to update time by one second */
   void time_update( struct time new_time )
   {
       ++new_time.seconds;
       if( new_time.seconds == 60) {
           new_time.seconds = 0;
           ++new_time.minutes;
           if(new_time.minutes == 60) {
               new_time.minutes = 0;
               ++new_time.hour;
               if(new_time.hour == 24)
                   new_time.hour = 0;
           }
       }
   }
/* function to change military time to standard time
void calculation(struct time mil_time)
{
if ((mil_time.hour>0)&&(mil_time.hour<=11))
{
mil_time.hour=mil_time.hour;
mil_time.ampm='A';
}
if ((mil_time.hour>=13)&&(mil_time.hour<=23))
{
mil_time.hour=mil_time.hour-12;
mil_time.ampm='P';
}

if((mil_time.hour>=0)&&(mil_time.hour<1))
{
mil_time.hour=12;
mil_time.ampm= 'A';
}
if (mil_time.hour==12)
{
mil_time.hour=12;
mil_time.ampm='P';
}

}
   main()
   {
       struct time current_time;

       printf("Enter the time (hh:mm:ss): ");
       scanf("%d:%d:%d",
           &current_time.hour,&current_time.minutes,&current_time.seconds);
       time_update ( current_time);
       calculation(current_time);
       printf("The new time is %02d:%02d:%02d %c ",current_time.hour,current_time.minutes,current_time.seconds,current_time.ampm)