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

you will practice programming with the C language. Much of your code will come i

ID: 3884594 • Letter: Y

Question

you will practice programming with the C language. Much of your code will come in handy for a sport diver who keeps a log that includes the duration of each dive.

Your task is to write a program that reads in dive times in the form of hh:mm (hours and minutes separated by a colon) and calculates the total dive time (i.e. the sum of all the log times), the average dive time and selects the longest dive time. The calculated outputs are all output in the form of hhh:mm or hh:mm (i.e. hours and minutes).

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct TIME

{

int mm;

int hh;

};

int converttoint(char str[],int dec)

{

int len,i;

dec=0;

len = strlen(str);

for(i=0; i<len; i++)

dec = dec * 10 + ( str[i] - '0' );

return dec;

}

void difference(struct TIME start, struct TIME stop, struct TIME *diff)

{

if(stop.mm > start.mm){

--start.hh;

start.mm += 60;

}

diff->mm = start.mm - stop.mm;

diff->hh = start.hh - stop.hh;

}

int main()

{

char start_time[10],end_time[10],ch='y';

char h[4],m[4];

int i=0,count=0;

int total_mm=0,total_hh=0;

struct TIME start,stop,diff;

int arr_hh[10],arr_mm[10];

while(1)

{

printf("Enter dive start time in format (HH:MM): ");

scanf("%s",start_time);

strcpy(h, strtok(start_time , ":"));

strcpy(m, strtok(NULL, ":"));

//printf(" %s:%s ", h, m);

start.hh=converttoint(h,start.hh);

start.mm=converttoint(m,start.mm);

//printf("=in int form %d:%d ", start.hh, start.mm);

  

printf("Enter dive end time in format (HH:MM): ");

scanf("%s",end_time);

strcpy(h, strtok(end_time , ":"));

strcpy(m, strtok(NULL, ":"));

//printf(" %s:%s ", h, m);

stop.hh=converttoint(h,stop.hh);

stop.mm=converttoint(m,stop.mm);

//printf("=in int form %d:%d ", stop.hh, stop.mm);

  

difference(start, stop, &diff);

/*printf(" TIME DIFFERENCE: %d:%d - ", start.hh, start.mm);

printf("%d:%d ", stop.hh, stop.mm);

printf("= %d:%d ", diff.hh, diff.mm);*/

arr_hh[i]=diff.hh;

if(arr_hh[i]<0) arr_hh[i]=arr_hh[i]*-1;

arr_mm[i]=diff.mm;

i++;

count++;

printf("Want to continue (y|n): ");

scanf(" %c",&ch);

if(ch=='n'||ch=='N') break;

}

printf("Dive Log " );

for(i=0;i<count;i++)

{

printf("%d : %d ",arr_hh[i],arr_mm[i]);

}

//printf("Total dive time: " );

for(i=0;i<count;i++)

{

total_mm+=arr_mm[i];

total_hh+=arr_hh[i];

}

int m3=(total_mm)%60;

int h3=(total_hh+((total_mm)/60));

printf(" Total dive time: %d:%d (HH:MM)", h3,m3);

  

for(i=0;i<count;i++)

{

total_mm+=arr_mm[i];

total_hh+=arr_hh[i];

}

total_mm=total_mm/(count+1);

total_hh=total_hh/(count+1);

m3=(total_mm)%60;

h3=(total_hh+((total_mm)/60));

printf(" Average dive time: %d:%d (HH:MM)", h3,m3);

  

int long_hh=arr_hh[0],long_mm=arr_mm[0];

for(i=1;i<count;i++)

{

if(long_hh<arr_hh[i])

{

long_hh=arr_hh[i];

}

else if(long_hh==arr_hh[i])

{

if(long_mm<arr_mm[i])

long_mm=arr_mm[i];

}

}

printf(" Longest dive time: %d:%d (HH:MM)", long_hh,long_mm);

return 0;

}

https://drive.google.com/open?id=0B482UDpVXbMvMTQ0aV9hYVZUQUU

for the image look up for the image that i have shared.