In: Computer Science
write a Program in C++
Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format:
t3 = addTime(t1, t2);
Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function:
display(t3);
code in c++
(code to copy)
#include <bits/stdc++.h>
using namespace std;
//declare struct
struct timeType{
int hour;
int min;
};
void display(struct timeType t){
cout<<t.hour<<":"<<t.min<<endl;
}
struct timeType addTime(struct timeType t1, struct timeType t2){
struct timeType x;
//add minutes
x.min = t1.min+t2.min;
//add hours
x.hour = t1.hour+t2.hour+x.min/60; //add carry from minutes
x.min=x.min%60; //reset minutes
x.hour=x.hour%24; //reset hours
return x;
}
int main()
{
struct timeType t1,t2;
t1.hour=19;
t1.min=40;
t2.hour=10;
t2.min=55;
//display t1
cout<<"t1: ";
display(t1);
//display t2
cout<<"t2: ";
display(t2);
//display t1+t2
cout<<"t1+t2: ";
display(addTime(t1, t2));
}
code screenshot
sample code output screenshot
..