Converting remainder of modulus to minutes

145 Views Asked by At

I have included my code below, Trying to take a speed from the user and a Distance they need to travel and calculate how many hours and minutes the trip will take.

But my minutes keep on displaying 0, I get the correct leftover from the modulus operator in the Temp variable. Is it something to do with casting/type of variables maybe?

I hope someone that is a bit better than me can see the fault.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Initialise the variables we will use
    float Speed, Distance, Remainder = 0.0;
    int Hours, Minutes, Temp = 0;
    
    //Tell the user what the program does
    printf("This program will take a distance and a speed and calculate your driving time!\n");

    //Take user input
    printf("please enter the speed you are driving at: ");
    scanf("%f", &Speed);

    printf("Enter the distance you will travel: ");
    scanf("%f", &Distance);

    //calculate the result`your text`
    Hours = Distance / Speed;   
    Temp = (int)Distance % (int)Speed;
    printf("temp = %d\n", Temp);
    Minutes = (Temp / 100) * 60;
    
    //Print out the result
    printf("If you are traveling %.2f km at the speed %.2f km/h you will arrive in %d hours and %d minutes\n", 
           Distance, Speed, Hours, Minutes);

    return 0;
}

Tried to change the types and cast some variables differently, but just keeps on delivering 0.

4

There are 4 best solutions below

0
chux - Reinstate Monica On

OP's approach determines Hours, Minutes independently from Distance and Speed and incurs edge and coherency issues. / 100 makes little sense.

Avoid residual and rounding issues by moving the Hours, Minutes calculations both from an integer derived from the same single FP value.

// Total time in minutes using FP math and then round.
long Total = lround(60.0 * Distance / Speed);
// Then separate with integer math
Hours = Total / 60;
Minutes = Total % 60;

Here, Hours, Minutes form a common single quantity called Total which is the total time in minutes and bypasses the / 100 mistake.

Also, Total = lround(60.0 * Distance / Speed) uses lround() as the time sought is likely better rounded to the nearest minute rather than truncated.

Simply as:

long MinuteTime = lround(60.0 * Distance / Speed);

printf("If you are traveling %.2f km at the speed %.2f km/h" 
    " you will arrive in %ld hours and %ld minutes\n", 
    Distance, Speed, MinuteTime/60, MinuteTime%60);
2
NoDakker On

Testing out your code, the issue resides within this block of code.

//calculate the result`your text`
Hours = Distance / Speed;   
Temp = (int)Distance % (int)Speed;
printf("temp = %d\n", Temp);
Minutes = (Temp / 100) * 60;

I'm not sure what the rational was for dividing "Temp" by "100", but that did not provide the proper remaining time.

With that, following is a refactored block of code.

//calculate the result`your text`
Hours = Distance / Speed;
Temp = (int)Distance - Hours * (int)Speed;  /* Get remaining distance to go */
printf("temp = %d\n", Temp);
Minutes = Temp * 60 / Speed;                /* Derive remaining time to cover remainder */

The main things to note were the derivation of the "Temp" value for the remaining kilometers and the derivation of the remaining minutes, placing the integer multiplication in the proper order. With those bits refactored, following was a test of the program.

craig@Vera:~/C_Programs/Console/Speed/bin/Release$ ./Speed 
This program will take a distance and a speed and calculate your driving time!
please enter the speed you are driving at: 90
Enter the distance you will travel: 120
temp = 30
If you are traveling 120.00 km at the speed 90.00 km/h you will arive in 1 hours and 20 minutes

Give that a try.

0
Vahid H.Amani On

Try:

Minutes = (Temp / Speed) * 60;

instead of:

Minutes = (Temp / 100) * 60;
0
abdallah mostafa On

The issue with your code is in how you calculate the minutes. You are dividing Temp by 100, which is not correct for calculating the remaining minutes.

Here's a corrected version of your code:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Initialize the variables we will use
    float Speed, Distance;
    int Hours, Minutes;

    // Tell the user what the program does
    printf("This program will take a distance and a speed and calculate your driving time!\n");

    // Take user input
    printf("Please enter the speed you are driving at: ");
    scanf("%f", &Speed);

    printf("Enter the distance you will travel: ");
    scanf("%f", &Distance);

    // Calculate the result
    float timeInHours = Distance / Speed;
    Hours = (int)timeInHours; // Get the whole hours
    Minutes = (timeInHours - Hours) * 60; // Get the remaining minutes

    // Print out the result
    printf("If you are traveling %.2f km at the speed %.2f km/h you will arrive in %d hours and %d minutes\n", 
           Distance, Speed, Hours, Minutes);

    return 0;
}

In this corrected code, I calculate the hours and minutes separately. I first calculate the total time in hours as a floating-point value (timeInHours) and then extract the whole hours and remaining minutes from it. The minutes are calculated by subtracting the whole hours from the total time in hours and converting the remaining fraction to minutes by multiplying by 60. This ensures that you get the correct result for both hours and minutes.