I need to know how can I add 2 "hours" as integer?
It is 24-h format
int add2Hours(int _time1,int _time2)
{
}
sample: 13:45 is: (hhmm) 1345
1345 + 30 returns 1415
Your time is in hhmm format, separate the hh and mm part. Then add the parts separately.
int add2hours(int _time1, int _time2)
{
int hh1, hh2, mm1, mm2;
int rHH,rMM, res;
hh1 = _time1/100;
hh2 = _time2/100;
mm1 = _time1 % 100;
mm2 = _time2 % 100;
rMM = mm1 + mm2;
rHH = rMM/60;
rMM = rMM % 60;
rHH = rHH + hh1 + hh2;
res = rHH*100 + rMM;
return res;
}
NOTE: This will not handle any time greater than 24hrs. e.g. if the inputs are 2345 and 30, the output will be 2415 instead of 15(0015). You have to handle it if you need.
If the function for adding time is declared as follows,
int add2Hours(int _time1,int _time2);
and the syntax of passing time is as follows,
hhmm (For example 2230)
Then you can add the times as follow,
temp1= _time1;
temp2= _time2;
m1 = temp1 % 100;
m2 = temp2 % 100;
h1 = temp1 / 100;
h2 = temp2 / 100;
m3 = m1 + m2;
m3 > 59 ? m3=m3%60, h3=1 : h3=0;
h3 = h1 + h2 + h3;
h3 > 23 ? h3=h3%24, d=1 : d=0; /* If more than 23 hours */
printf("\nThe time is %d-%d-%d",d,h3,m3);
First convert the time to a common domain(sec/millisec...). Then add and make the result to the required format.
How about this?
int add2Hours(int _time1,int _time2)
{
int minutes = (_time1%100 + _time2%100)
int time = (((minutes/60)+(_time1/100 + _time2/100))%24)*100 + minutes%60;
return time;
}
Add the hours together. 13 + 0.
Get the minutes of each time by subtracting the time with the time/100*100. Note that integer arithmetic in C truncates. Example:
m = time - (time/100*100)
m = 1345 - (1345/100*100)
m = 1345 - (13*100)
m = 1345 - 1300
m = 45
Add the minutes together. 45 + 30 = 75.
Add "the minute sum/60" hours to the result. I.e. 75/60 = 1.
Then add "the minute sum modulo 60" minutes to the same result. I.e. 75%60 = 15.
Thanks guys... here is my function!
int Add2Times (int _time1, int _time2)
{
int hour = _time1 / 100 + _time2 / 100;
int min = _time1 % 100 + _time2 % 100;
hour += min / 60;
min = min % 60;
hour = hour % 24;
return hour * 100 + min;
}
Related
I've spent a good hour on this, but I can't create a formula that works for my c program. (I have a new programmer).
I have to convert UTC time to its respective time in a particular city. My code works perfectly except here. Here it gives me the wrong answer. I can't wrap my head around it (I created a formula but it makes no sense to me).
In my program, time is entered as 24 hour time. 9AM = 900, 12PM = 1200, 12am = 0 etc.
If we are asked to convert 2359 to Eucla time (UTC +845) my program outputs 804. The correct answer is 844.
I figured out how to calculate 844, but I make no sense of it.
2359 + 845 = 3204 (adding the timezone offset 845 to the UTC time)
3204 - 60 = 3144 (minus 60 for some reason [I followed my time overflow formula]
3144 - 2400 = 2400 (minus 2400 because time cannot be more than 2359)
How my program works
First plus UTC and offset time
calculatedTime = UTC + offset;
Then under that
if (calculatedTime < 2359) {
calculatedTime = calculatedTime - 2400;
}
I also have another function which checks for overflow time underneath
if (((calculatedTime > 59) && (calculatedTime < 99)) || ((calculatedTime > 159) && (calculatedTime < 199))) {
// All the way to 2359
calculatedTime = calculatedTime - 60 + 100;
}
You need to separate the time into hours and minutes. Then add the time zone offsets to the hours and minutes separately. Handle roll-over. Finally, recombine the hours and minutes into the final answer.
Like this:
int main(void)
{
// inputs
int time = 2359;
int zone = 845;
// separate hours and minutes
int timeHours = time / 100;
int timeMinutes = time % 100;
int zoneHours = zone / 100;
int zoneMinutes = zone % 100;
// add the hours and minutes
int hours = timeHours + zoneHours;
int minutes = timeMinutes + zoneMinutes;
// handle the rollover conditions
if (minutes > 60) {
minutes -= 60;
hours++;
}
if (hours > 24) {
hours -= 24;
}
// recombine the hours and minutes
int adjustedTime = hours * 100 + minutes;
printf("%d\n", adjustedTime);
}
Note that this code only works for timezones with positive offsets. You'll need to figure out how to make it work for negative time zones.
OP's code has various off-by-one errors.
// if (((calculatedTime > 59) && (calculatedTime < 99)) ||
// ((calculatedTime > 159) && (calculatedTime < 199))) {
if (((calculatedTime > 59) && (calculatedTime < 100)) ||
((calculatedTime > 159) && (calculatedTime < 200))) {
// if (hours > 24) {
// hours -= 24;
// }
if (hours >= 24) {
hours -= 24;
}
Also code has more clarity using values like 60, 100
if (((calculatedTime >= 60) && (calculatedTime < 100)) ||
((calculatedTime >= 60*2) && (calculatedTime < 100*2))) {
Yet OP's approach fails with negative numbers.
To cope with positive and negative time values, split the "hhmm" time into a hours and minutes. Look for conditions of "minute" overflow. I recommend 2 helper functions to split and combine results.
#include <stdio.h>
#include <stdlib.h>
void hhmm_split(int hhmm, int *hour, int *min) {
*hour = hhmm / 100;
*min = hhmm % 100;
}
/* `min` may be outside the primary range of (-60 ... 60) */
int hhmm_combine(int hour, int min) {
hour += min / 60;
min %= 60;
if (hour < 0 && min > 0) {
min -= 60;
hour++;
} else if (hour > 0 && min < 0) {
min += 60;
hour--;
}
hour %= 24;
return hour * 100 + min;
}
Test code
void hhmm_add(int t1, int t2) {
int t1_hh, t1_mm, t2_hh, t2_mm;
hhmm_split(t1, &t1_hh, &t1_mm);
hhmm_split(t2, &t2_hh, &t2_mm);
int sum = hhmm_combine(t1_hh + t2_hh, t1_mm + t2_mm);
printf("t1:% 05d + t2:% 05d = sum:% 05d\n", t1, t2, sum);
}
int main(void) {
hhmm_add(2359, 845);
hhmm_add(2359, -845);
hhmm_add(-2359, 845);
hhmm_add(-2359, -845);
}
Output:
t1: 2359 + t2: 0845 = sum: 0844
t1: 2359 + t2:-0845 = sum: 1514
t1:-2359 + t2: 0845 = sum:-1514
t1:-2359 + t2:-0845 = sum:-0844
Essentially the program I've made adds time when the times are listed in the kind of military format (eg. 2300 is equal to 11pm) and then a duration is added (eg. 345, 3 hours 45 minutes.)
I've come across an issue in that when you add time to something that surpasses midnight it will continue to add past that. So for instance if you add 2 hours (200) to 11pm (2300) you will get a result of 2500 which is illogical for this purpose.
What I need my program to do is reach 2400 (midnight) and then reset to 0 but continue to add the remaining amount that needed to be added.
I've been trying to figure this out but I cannot figure out how I'm going to make it add what remains after you reset it to 0.
Since minutes are added modulo 60 and hours modulo 24, you need to handle them separately.
Something like
int add_time(int old_time, int addition)
{
/* Calculate minutes */
int total_minutes = (old_time % 100) + (addition % 100);
int new_minutes = total_minutes % 60;
/* Calculate hours, adding in the "carry hour" if it exists */
int additional_hours = addition / 100 + total_minutes / 60;
int new_hours = (old_time / 100 + additional_hours) % 24;
/* Put minutes and hours together */
return new_hours * 100 + new_minutes;
}
Break 24 hour/minute encode as a 4-digit int into hours and minutes. Avoid assuming values are in their primary range.
Add
Re-form time into the primary range.
Example
typedef struct {
int hour; // Normal range 0 - 23
int minute; // Normal range 0 - 59
} hhmm;
hhmm int_to_hhmm(int src) {
src %= 2400;
if (src < 0) src += 2400;
hhmm dest = { src / 100, src % 100 };
return dest;
}
int hhmm_to_int(hhmm src) {
src.hour += src.minute / 60;
src.minute %= 60;
if (src.minute < 0) {
src.minute += 60;
src.hour--;
}
src.hour %= 24;
if (src.hour < 0) {
src.hour += 24;
}
return src.hour * 100 + src.minute;
}
int add_mill_format_times(int t0, int t1) {
hhmm hhmm0 = int_to_hhmm(t0);
hhmm hhmm1 = int_to_hhmm(t1);
hhmm sum = { hhmm0.hour + hhmm1.hour, hhmm0.minute + hhmm1.minute };
return hhmm_to_int(sum);
}
you can do
addTime(int time1,int time2{
int first,last,addH,addM
last = func1(time1) + func2(time2);
last=last1+last2;
if(last > 60){
addH=last/60;
addM=last%60;
}
first=func2(time1)+func2(time2);
first+= addH;
last += addM;
first/=24;
return first*100 + addM;
}
func2(int time){
int i;
for(i=0;i<2;i++)
time/=10;
return time;
}
func1(int time){
int l=time%10;
time/=10;
int m=time%10;
return m*10 + l;
}
I have a loop for my clock, that I am trying to make an add 30 sec if keypad sends an x value of 30. It then displays on the LED display. It works, but I seem to be having an issue with adding 30 seconds and over 59 sec. The way it worked originally was it kept adding 30 to 60 to 90 + 120, etc. But I want it to roll over every 60 secs, when I add 30 sec. If I should rewrite the code, I am all game, but any leads would be helpful. Know this code is for an embedded item and is only a snippet. the inital set time is added before set loop, and is held as the value for test.
n=test;
for(i=0;i<n;i++){
q = test %100; /*q makes real time clock work in 60 sec decriments*/
if(q==0){
test = test - 40;
}
key = KeypadReadPort();
x = keys[key];
if(x==30){ /*add 30 seconds during count*/
n = n + 30;
sec1=(test+30)%90;
if(sec1>=60){
min=q;
sec2=(test+30)%60;
test=sec2+(100+q);
} else if (sec1<60){
test=test+30;
}
SevenSegWrite(test);/* display result on LED*/
}
test = test - 1; /*count down tick*/
SevenSegWrite(test);
OSTimeDlyHMSM(0, 0, 1, 0);/*1 second tick*/
if(test==0){
for(i=0;i<750;i++){ /*buzzer*/
PTT = PTT | 0x20;
OSTimeDlyHMSM(0,0,0,1);
PTT = PTT & 0xDF;
OSTimeDlyHMSM(0,0,0,1);
}
}
}
test=0;
I would do it this way. It works for me. You have a great start! You just need to break down your clock numbers a bit.
if(start<15){
min=0; /*microwave keypad numeric input method*/
n = test;
for(i=0;i<n;i++){
q = test %100; /*q makes real time clock work in 60 sec decriments*/
if(q==0){
test = test - 40;
}
key = KeypadReadPort();
x = keys[key];
if(x==30){ /*add 30 seconds during count*/
d4 = test % 10;
d3 = (test / 10) % 10;
d2 = (test / 100) % 10;
d1 = (test / 1000) % 10;
min=(d1*1000)+(d2*100);
sec1=(d3*10)+d4;
n = n + 30;
sec2=(sec1+30)%90;
if(sec2>=60){
min=min+100;
sec1=(sec1+30)%60;
test=sec1+(min);
} else if (sec2<60){
test=test+30;
}
SevenSegWrite(test);
}
test = test - 1; /*count down tick*/
SevenSegWrite(test);
OSTimeDlyHMSM(0, 0, 1, 0);
if(test==0){
for(i=0;i<750;i++){ /*buzzer*/
PTT = PTT | 0x20;
OSTimeDlyHMSM(0,0,0,1);
PTT = PTT & 0xDF;
OSTimeDlyHMSM(0,0,0,1);
}
}
Days ago I had a job interview were they ask me how I would calculate the sum of all the numbers multiples of 2 or 5 from 1 to 10000 using a the c language. I did this:
int mult2_5()
{
int i, sum=0;
for(i = 1; i <= 10000; i++)
if(i % 2 == 0 || i % 5 == 0)
sum += i;
return sum;
}
I as wonder if it was any faster implementation that this one?
The modulus operator is inefficient. A more faster implementation would be something like this:
int multiply2_5(int max)
{
int i, x2 = 0,x5 = 0,x10 = 0;
for(i = 2; i < max; i+=2) x2 += i; // Store all multiples of 2 O(max/2)
for(i = 5; i < max; i+=5) x5 += i; // Store all multiples of 3 O(max/5)
for(i = 10; i < max; i+=10) x10 += i; // Store all multiples 10; O(max/10)
return x2+x5-x10;
}
In this solution I had to take out multiples of 10 because, 2 and 5 have 10 as multiple so on the second loop it will add multiples of 10 that already been added in the first loop; The three loops combine have O(8/10 max).
Another even better solution is if you take a mathematical approach.
You are trying to sum all numbers like this 2 + 4 + 6 + 8 ... 10000 and 5 + 10 + 15 +20 + ... 10000 this is the same of having 2 * (1 + 2 + 3 + 4 + … + 5000) and 5 * ( 1 + 2 + 3 + 4 + ... + 2000), the sum of 'n' natural number is (n * (n + 1)) (source) so you can calculate in a constant time, as it follows:
int multiply2_5(int max)
{
// x = 2 + 4 + 6 + ... = 2 * (1 + 2 + 3 +...)
// y = 5 + 10 + 15 + ... = 5 * (1 + 2 + 3 +...)
// The sun of n natural numbers is sn = (n (n + 1)) / 2
int x2 = max/ 2; // 2 * ( 1 +2 + 3 … max/2)
int x5 = max /5; // 5 * ( 1 +2 + 3 … max/5)
int x10 = max/ 10;
int sn2 = 0.5 * (x2 * (x2+1)); // (n * (n + 1)) / 2
int sn5 = 0.5 * (x5 * (x5+1));
int sn10 = 0.5 * (x10 * (x10+1));
return (2*sn2) + (5 *sn5) - (10*sn10);
}
As mentioned in an earlier answer, explicitly looping through the relevant multiples is better than testing the remainder each loop. But it is not necessary to calculate the multiples of 10 and subtract. Just start at 5 and step by 10 to skip them all together.
int multiply2_5b(int max)
{
int i, x2 = 0,x5 = 0;
for (i = 2; i < max; i += 2) x2 += i; // Sum all multiples of 2
for (i = 5; i < max; i += 10) x5 += i; // Sum all odd multiples of 5
return x2 + x5;
}
Just work it out on paper first, if that's what you mean by "faster".
$2\sum_{1<=2k<=10000}k + 5\sum_{1<=5k<=10000} - 10\sum_{1<=10k<=10000}k$
Sorry, my SO equation-fu is weak...Anyway this route will give you something you can almost handle on paper: 5000*6001 after reducing a few steps
int
mult2_5(void)
{
return 5000*6001;
}
Project Euler problem 1 is very similar. There's lots of folks who've posted their solution to this one.
This can just be done using math. Something like 2 * sum(1 to 5000) + 5 * sum(1 to 2000) - 10 * sum(1 to 1000). off-by-one errors left as exercise.
I almost got to a pure and simple multiplication by doing a simple loop that starts with 35 (sum of 2 + 4 + 5 + 6 + 8 + 10) with a step of 60, as that's how much your result will increase by when you take the next lot e.g. 12 + 14 + 15 + 16 + 18 + 20 etc. for(int i=35;i<5976;i=i+60) { sum=sum+i }
The 5976 comes from 5975 being the last row of numbers that end in 1000 i.e. 992 + 994 + 995 + 996 + 998 + 1000.
So it turns out that this loop runs 100 times, increasing the sum by 35 the first turn and increasing by 60 the remaining 99 times. Which is now reducable to a simple multiplication or such.
#include <stdio.h>
int main(void)
{
int days, hours, mins;
float a, b, c, total, temp, tempA, tempB;
a = 3.56;
b = 12.50;
c = 9.23;
total = a+b+c;
days = total / 24;
temp = total/24 - days;
hours = temp * 24;
tempA = temp*24 - hours;
mins = tempA*60;
while (hours >= 24)
{
hours= hours-24;
days +=1;
}
while ( mins >= 60)
{
mins=mins-60;
hours +=1;
}
printf("days:%d\n", days);
printf("hours:%d\n", hours);
printf("mins:%d\n", mins);
return 0;
}
I wanted to convert decimal hours to real time and I can do it fine but I wanted to increase days hours if the hours is beyond 24 and if mins is beyond 60mins.
the while loop does subtract and it does print out the new value but the hours / days aren't getting compounded.
It was 1 day 1 hour 77mins
I wanted it to read 1 day 2 hours 17mins
but I'm getting 1 day 1 hour 17 mins.
Using the modulus operator will make your life much easier: it will give the remainder of a division.
int total;
/* a=; b=; c=; assignments */
total = a+b+c;
mins = total % 60;
total /= 60;
hours = total % 24;
days = total / 24;
Here is a simpler implementation of what you are trying to do:
void TimeFix(int &days, int &hours, int &mins)
{
hours += mins/60;
mins %= 60;
days += hours/24;
hours %= 24;
}
Running your program I'm getting:
days:1
hours:1
mins:17
and that's what I expect considering that total should be 25.29.
It works fine, your math is just a little off. (= (+ 3.56 12.50 9.23) 25.29), not 26.29.
Instead of a while loop you can use division:
days += hours / 24
hours %= 24
Also, do your minutes-to-hours stuff before your hours-to-days stuff.