Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a structure called time. Its three members, all type int, should be calle

ID: 3802232 • Letter: C

Question

Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a C++ program that contains a function accepting an integer parameter (number of seconds since the start of the day) and returns the structure time with its members set up accordingly. The function add in your program must add two time values stored in the time structures passed by reference and return the time structure corresponding to the result. The function compare must compare two time values stored in the time structures passed by reference and return one of the integer values (-1, 0, +1), depending on the comparison. Create several time instances and show the usage of these function in your program.

Explanation / Answer

#include<iostream>
using namespace std;

typedef struct
{
   int hours;
   int minutes;
   int seconds;

}Time;
  
Time* getTime(int seconds );
Time* add(Time &time1, Time &time2);
int compare(Time &time1, Time &time2);

int main()
{
Time* t1 = getTime(0);
Time* t2 = getTime(30);
Time* t3 = getTime(60);
Time* t4 = getTime(90);
Time* t5 = getTime(130);
Time* t6 = getTime(3600);
Time* t7 = getTime(3660);
Time* t8 = getTime(3666);
Time* t9 = getTime(7200);
Time* t10 = getTime(7320);
Time* t11 = getTime(7326);

cout << "0" << " -" << t1->hours << " " << t1->minutes << " " << t1->seconds <<endl;
cout << "30" << " -" << t2->hours << " " << t2->minutes << " " << t2->seconds <<endl;
cout << "60" << " -" << t3->hours << " " << t3->minutes << " " << t3->seconds <<endl;
cout << "90" << " -" << t4->hours << " " << t4->minutes << " " << t4->seconds <<endl;
cout << "130" << " -" << t5->hours << " " << t5->minutes << " " << t5->seconds <<endl;
cout << "3600" << " -" << t6->hours << " " << t6->minutes << " " << t6->seconds <<endl;
cout << "3660" << " -" << t7->hours << " " << t7->minutes << " " << t7->seconds <<endl;
cout << "3666" << " -" << t8->hours << " " << t8->minutes << " " << t8->seconds <<endl;
cout << "7200" << " -" << t9->hours << " " << t9->minutes << " " << t9->seconds <<endl;
cout << "7320" << " -" << t10->hours << " " << t10->minutes << " " << t10->seconds <<endl;
cout << "7326" << " -" << t11->hours << " " << t11->minutes << " " << t11->seconds <<endl;


Time* sum = add( *t2 ,*t3);
Time* sum2 = add( *t6 ,*t7);
cout<< "Adding t2 and t3 - " << sum->hours << " " << sum->minutes << " " << sum->seconds << endl;
cout<< "Adding t6 and t7 - " << sum2->hours << " " << sum2->minutes << " " << sum2->seconds << endl;

cout<< "Compare sum and t4 - " << compare( *sum, *t4) <<endl;
cout<< "Compare sum and sum2 - " << compare( *sum, *sum2) <<endl;
cout<< "Compare t11 and t10 - " << compare( *t11, *t10) <<endl;

return 0;
}

Time* getTime(int seconds)
{
Time* time = NULL;
int hour = 0, minute = 0, sec = 0;

if(seconds < 0){
return NULL;
}

//time = (Time*)malloc(sizeof(Time));
time = new Time;

hour = (seconds / 3600);
minute = ((seconds % 3600) / 60);
sec = ((seconds % 3600) % 60);

time->hours = hour;
time->minutes = minute;
time->seconds = sec;

/********************************************************
* Number of seconds in a day = (24 * 3600) = 86400
* If the seconds passed to this function exceeds this
* value. We need to convert the hours value such that
* it lies between (1-24)
* e.g If the value passed to this function is (25 * 3600)
* which represent the next day 1 AM time
* time->hours = 25
* time->minutes = 0
* time->seconds = 0
* But we want time->hours = 1, so if we are considering
* such cases too then following line should also be
* added to the code:
*
* time->hours = (time->hours) % 24 ;
*
* For now i am not adding it.
* ******************************************************/

return time;
}


Time* add(Time &time1, Time &time2)
{
int total_seconds = 0;
Time* newTime = NULL;

total_seconds = (time1.seconds + time2.seconds) + ((time1.minutes + time2.minutes) * 60) + ((time1.hours + time2.hours)*3600);

newTime = getTime(total_seconds);

return newTime;
}

int compare(Time &time1, Time &time2)
{
int total_seconds_1 = 0;
int total_seconds_2 = 0;


total_seconds_1 = (time1.seconds + (time1.minutes * 60) + (time1.hours * 3600) );
total_seconds_2 = (time2.seconds + (time2.minutes * 60) + (time2.hours * 3600) );


if( total_seconds_1 == total_seconds_2){
return 0;
}
else if( total_seconds_1 > total_seconds_2){
return 1;
}
else{
return -1;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote