//import library
#include <stdio.h>
#include <stdlib.h>
//declare variable structure
struct time{
int hour;
int min;
int sec;
}startTime, endTime, different, elapsed;
//mould struct and compute elapsedTime
struct time elapsedTime(struct time start, struct time end){
int secondStart, secondEnd, secondDif;
secondEnd = end.hour * 60 * 60 + end.min * 60 + end.sec;
secondStart = start.hour * 60 * 60 + start.min * 60 + start.sec;
if (secondEnd>secondStart)
secondDif = secondEnd - secondStart;
else
secondDif = secondStart - secondEnd;
different.hour = secondDif / 60 / 60;
different.min = secondDif / 60;
return different;
}
//main function
void main(){
printf("Enter start time (Hour Minute Second) using 24 hours system : ");
scanf("%d %d %d", startTime.hour, startTime.min, startTime.sec);
printf("Enter end time (Hour Minute Second) using 24 hours system : ");
scanf("%d %d %d", endTime.hour, endTime.min, endTime.sec);
elapsed = elapsedTime(startTime, endTime);
}
Can someone help me check and run the code to check whether it is working or not?
You have a mistakes in main function, you should use in scanf int * instead of int so you must add &, you can see below:
//main function
void main(){
printf("Enter start time (Hour Minute Second) using 24 hours system : ");
scanf("%d %d %d", &startTime.hour, &startTime.min, &startTime.sec);
printf("Enter end time (Hour Minute Second) using 24 hours system : ");
scanf("%d %d %d", &endTime.hour, &endTime.min, &endTime.sec);
elapsed = elapsedTime(startTime, endTime);
}
I assume you would want to calculate different.sec so I think the correct calculation of the different time is:
secPerHr = 60 * 60;
secPerMin = 60;
if (secondDif >= secPerHr) {
different.hour = secondDif / secPerHr;
secondDif -= different.hour * secPerHr;
}
if (secondDif >= secPerMin) {
different.min = secondDif / secPerMin;
secondDif -= different.min * secPerMin;
}
different.sec = secondDif;
Also, for testing purposes you would probably want to display the result in your main function.
Related
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
No need for any floating point math
Before subtracting, break time into hours and minutes
int startTime_hours = startTime/100;
int startTime_mins = startTime%100;
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);
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("%04d", &startTime);
printf("End time\t: ");
scanf("%04d", &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;
}
I am trying to find differentiation between two date(i.e. 14:49:41 and 15:50:42) using below code:
Action()
{
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
};
int rc; // return code
struct tm date1;
struct tm date2;
long time_difference; // the number of time ticks (seconds) that separate date1 and date2.
int hours, minutes, seconds;
// Save example dates to a parameter.
// capture these values using web_reg_save_param or similar.
// date format: hh:mm:ss
lr_save_string("14:49:41", "Param_Date1");
lr_save_string("15:50:42", "Param_Date2");
// Read the values from the string into the date variables
rc = sscanf(lr_eval_string("{Param_Date1}"), "%d:%d:%d",&date1.tm_hour, &date1.tm_min, &date1.tm_sec);
// Repeat the above steps for Date2
rc = sscanf(lr_eval_string("{Param_Date2}"), "%d:%d:%d", &date2.tm_hour, &date2.tm_min, &date2.tm_sec);
time_difference = mktime(&date2) - mktime(&date1);
lr_output_message("Total number of seconds difference: %d", time_difference);
// Calculate time difference in hours, minutes and seconds.
hours = time_difference/3600;
time_difference = time_difference - (hours * 3600);
minutes = time_difference/60;
time_difference = time_difference - (minutes * 60);
seconds = time_difference;
lr_output_message("Hours: %d, Minutes: %d, Seconds: %d", hours, minutes, seconds);
return 0;
}
Actual output should return : Hours: 1, Minutes: 1, Seconds: 1
But output returns : Hours: 0, Minutes: 0, Seconds: 0
Please help me fix this problem. Or Else any other alternative achieve it?
hours, minutes and seconds should not be declared as Integers as you are dividing the value by 3600. If you declare them as floating point numbers it may work. Other than that everything looks good
The difference between two times in seconds is easy to compute:
int secs1 = ((time1.hour * 60) + time1.min) * 60 + time1.sec;
int secs1 = ((time2.hour * 60) + time2.min) * 60 + time2.sec;
int sec_dif = secs1 - secs2;
Or this way:
int sec_dif =
((time1.hour - time2.hour) * 60 + (time1.min - time2.min)) * 60 + (time1.min - time2.min);
int min_dif = sec_dif / 60;
int hour_dif = sec_dif / (60 * 60);
No need to bother with converting the times into time_t types.
Hi pretty new to coding in c, but would love your help in my following coding problem.
I want to add two times (that are in twenty four hour time notation already).
Currently they are both integers and the arithmatic addition function is great for whole hour (e.g. 800+1000), however because our / computer's numbers are base ten, it will not roll over to the next hour after 60min which leads to problems with addition.
I'm not sure if the modulus % can solve this? Ideally I would like to use simple c coding (that I understand), and not start importing timing keys into the program.
e.g.
#include <stdio.h>
int main (void)
{
int time1 = 1045; // 10:45am in 24hour time
printf("Time %d ",time1);
int time2 = 930; //9 hours & 30min
printf("+ time %d", time2);
int calc = time1 + time2;
printf(" should not equal ... %d\n", calc);
printf("\nInstead they should add to %d\n\n", 2015); //8:15pm in 24hr time
return 0;
}
Yes, you're correct that modulo division is involved. Remember, that is remainder division. This is more worthy as a comment since supplying a complete answer for problems like this is generally frowned upon, but it's too long for that; this should get you started:
#include <stdio.h>
int main(void)
{
// Assuming the given time has the format hhmm or hmm.
// This program would be much more useful if these were
// gathered as command line arguments
int time1 = 1045;
int time2 = 930;
// integer division by 100 gives you the hours based on the
// assumption that the 1's and 10's place will always be
// the minutes
int time1Hours = time1 / 100; // time1Hours == 10
int time2Hours = time2 / 100; // time2Hours == 9
// modulus division by 100 gives the remainder of integer division,
// which in this case gives us the minutes
int time1Min = time1 % 100; // time1Min == 45
int time2Min = time2 % 100; // time2Min == 30
// now, add them up
int totalHours = time1Hours + time2Hours; // totalHours = 19
int totalMin = time1Min + time2Min; // totalMin = 75
// The rest is omitted for you to finish
// If our total minutes exceed 60 (in this case they do), we
// need to adjust both the total hours and the total minutes
// Clearly, there is 1 hour and 15 min in 75 min. How can you
// pull 1 hour and 15 min from 75 min using integer and modulo
// (remainder) division, given there are 60 min in an hour?
// this problem could be taken further by adding days, weeks,
// years (leap years become more complicated), centuries, etc.
return 0;
}
I used this for a long time...
// convert both times hhmm to minutes an sum minutes
// be sure to do integer division
int time1m = ((time1 / 100) * 60)+(time1 % 100);
int time2m = ((time2 / 100) * 60)+(time2 % 100);
int sumMin = time1m + time2m;
// convert back to hhmm
int hhmm = ((sumMin / 60) * 100)+(sumMin % 60);
You can also include a day, as time is of 24 hour format.
#include <stdio.h>
int main()
{
int t1=2330;
int t2=2340;
int sum=((t1/100)*60)+(t1%100)+((t2/100)*60)+(t2%100);
int day=sum/(24*60);
sum = sum % (24*60);
int hours=sum/(60);
int mins=sum % 60;
printf("days = %d \t hours = %d \t mins=%d\n",day, hours, mins);
return 0;
}
I'm working through an example for Advanced Programming in the Unix Environment and the following questions was asked:
If the process time is stored as a 32bit signed integer, and the system counts 100 ticks per second, after how many days will the value overflow?
void proc_ovf()
{
int sec = 60;
int min = 60;
int hour = 24;
int tick = 100;
int epoch_time = (((INT_MAX / (sec * tick)) / min) / hour);
struct tm * timeinfo;
time_t epoch_time_as_proc_t = epoch_time;
timeinfo = localtime(&epoch_time_as_proc_t);
printf("3] overflow date of proc: %s", asctime(timeinfo));
}
Is the following solution a reasonable calculation for how many days before overflow?
(((INT_MAX / (sec * tick)) / min) / hour)
This calculation yielded 248 days.
248 days looks good.
But your code doesn't. Your variables have the wrong names. They should be:
int ticks_per_second = 100;
int seconds_per_minute = 60;
int minutes_per_hour = 60;
int hours_per_day = 24;
int ticks = INT_MAX;
int seconds = ticks / ticks_per_second;
int minutes = seconds / seconds_per_minute;
int hours = minutes / minutes_per_hour;
int days = hours / hours_per_day;
printf("overflow after %d days\n", days);
The above code takes care of mentioning the measurement units. Can you see how nicely the measurement units cancel out in each line of the second part of the code?
I am trying to calculate time in C language and i have the following program:
#include <stdio.h>
#include <time.h>
int main(void) {
int hours, minutes;
double diff;
time_t end, start;
struct tm times;
times.tm_sec = 0;
times.tm_min = 0;
times.tm_hour = 0;
times.tm_mday = 1;
times.tm_mon = 0;
times.tm_year = 70;
times.tm_wday = 4;
times.tm_yday = 0;
time_t ltt;
time(<t);
struct tm *ptm = localtime(<t);
times.tm_isdst = ptm->tm_isdst;
printf("Start time (HH:MM): ");
if((scanf("%d:%d", ×.tm_hour, ×.tm_min)) != 2){
return 1;
}
start = mktime(×);
printf("End time (HH:MM): ");
if((scanf("%d:%d", ×.tm_hour, ×.tm_min)) != 2){
return 1;
}
end = mktime(×);
diff = difftime(end, start);
hours = (int) diff / 3600;
minutes = (int) diff % 3600 / 60;
printf("The difference is %d:%d.\n", hours, minutes);
return 0;
}
The program works almost ok:
Output 1:
./program
Start time (HH:MM): 05:40
End time (HH:MM): 14:00
The difference is 8:20.
Output 2:
./program
Start time (HH:MM): 14:00
End time (HH:MM): 22:20
The difference is 8:20.
Output 3:
/program
Start time (HH:MM): 22:20
End time (HH:MM): 05:40
The difference is -16:-40.
As you can see, I got -16:-40 instead of 7:20.
I cannot figure out how to fix this.
If end is after midnight and start before, add 24 hours to the end value:
if( end < start )
{
end += 24 * 60 * 60 ;
}
diff = difftime(end, start);
Note too that all the code related to mktime and tm struct is unnecessary. Those are useful when you require time normalisation (for example if you set tm_hour to 25, mktime will generate a time_t value that is 0100hrs the following day, rolling over the month and year too if necessary), but here you are dealing with just time of day in hours and minutes, so you need just:
int hour ;
int minute ;
if((scanf("%d:%d", &hour, &minute)) != 2){
return 1;
}
start = (time_t)((hour * 60 + minute) * 60) ;
printf("End time (HH:MM): ");
if((scanf("%d:%d", &hour, &minute)) != 2){
return 1;
}
end = (time_t)((hour * 60 + minute) * 60) ;