Home > Mobile >  How do I properly compute and convert the starting time and ending time of the call in minutes?
How do I properly compute and convert the starting time and ending time of the call in minutes?

Time:12-09

The start and end time are based on a 24 hour clock format. The task is that we will input the start and the end time then we will compute the length of the call and convert the result in minutes.

Sample output: Start time: 1810 End time: 2000 Length of call: 110 minutes

Here's what I did try doing. First, I tried to minus the start and end time and automatically turn the answer into positive. Now if the total result(resultMain) is greater than 120, it will multiply the result to (.60). Else if the result is greater than 60 and less than 120, then it will just get minus 40 instead of it getting multiplied by (.60). My problem is that my result is inconsistent, sometimes the answer is correct but sometimes it is wrong.

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
    int startTime, endTime, result1, result2;
    double totalTime1, totalTime2, resultMain;
    
    printf("\nPLDT Telephone Call Charge\n");
    printf("\nStart time\t: ");
    scanf("%d", &startTime);
    printf("End time\t: ");
    scanf("%d", &endTime);

    totalTime1 = startTime - endTime;
    resultMain = fabs(totalTime1);
    
    if(resultMain >= 120){
        
    totalTime2 = resultMain * .60;
    result1 = ceil(totalTime2); 
    result2 = fabs(result1);
    
    printf("Length of call\t: %d minutes\n", result2);
    }else if(resultMain >= 60 && resultMain < 120){

        totalTime2 = resultMain - 40;
        result1 = ceil(totalTime2);
        result2 = fabs(result1);

        printf("Length of call\t: %d minutes\n", result2);
    }else{
        totalTime2 = resultMain;
        result1 = ceil(totalTime2);
        result2 = fabs(result1);
        
        printf("Length of call\t: %d minutes\n", result2);
    }
   return 0;
    }

Example of correct answer: Start time: 0123 End time: 0224 Length of call: 61 minutes

Example of wrong answer: Start time: 0852 End time: 0906 Length of call: 54 minutes

Example of wrong answer: Start time: 0805 End time: 1210 Length of call: 243 minutes

CodePudding user response:

No need for any floating point math

Before subtracting, break time into hours and minutes

int startTime_hours = startTime/100;
int startTime_mins = startTime0;
startTime_mins  = startTime_hours*60; // startTime_mins is now the total minutes.

The difference is the end minus the start

int diff = endTime_mins - startTime_mins;

When difference is negative, add a day worth of time

Example: start time just before midnight and the end time after midnight.

if (diff < 0) {
  diff  = 24*60;
}

Only 1 case needed for printing

printf("Length of call\t: %d minutes\n", diff);

CodePudding user response:

You can use this algorithm to calculate the difference in minutes between two times:

const MINS_PER_HR = 60, MINS_PER_DAY = 1440

startx = starthour * MINS_PER_HR   startminute
endx = endhour * MINS_PER_HR   endminute

duration = endx - startx
if duration < 0:
    duration = duration   MINS_PER_DAY

See: Algorithm needed to calculate difference between two times

This code will implement it for you:

#include <stdio.h>

int main()
{
    int startTime, endTime, startHour, startMin, endHour, endMin, duration;
    
    printf("\nPLDT Telephone Call Charge\n");
    printf("\nStart time\t: ");
    scanf("d", &startTime);
    printf("End time\t: ");
    scanf("d", &endTime);

    startHour = startTime / 100;
    startMin  = startTime % 100;
    endHour   = endTime / 100;
    endMin    = endTime % 100;
    duration  = (endHour * 60   endMin) - (startHour * 60   startMin);
    if (duration < 0)
        duration  = 60 * 24;
    printf("Length of call\t: %d minutes\n", duration);

    return 0;
}
  • Related