1- Write a program that asks the user to input a number of seconds and displays the corresponding time in the form: hours:minutes:seconds. The total number of hours should not exceed 23.
Example: if the input is 7275, the output will be: 2:1:15
Hint: if t is the time in seconds, its components are:
Hours= (t/3600)%24
Minutes= (t/60)%60
Seconds= t%60

2- In many operations on time objects, you need to convert the time into seconds and perform the operation. Write a function that converts the time with hours, minutes, and seconds components into seconds. The function has the specifications:
int timeinseconds( int hours, int minutes, int seconds).

3- Implement the class Time with the following members:
a- The member variables hours, minutes, and seconds are private. The constructors will ensure that hours< 24, minutes<60, and seconds<60.
b- A constructor with three arguments representing respectively the hours, minutes, and seconds.
Add to this constructor features to adjust the time such that hours< 24, minutes<60, and seconds<60. For example, the time (25, 30,65) should be adjusted to (1, 31, 5).
c- A constructor with one argument assumes its argument is in seconds.
d- A copy constructor that uses a call by reference.
e- Readers and writers member functions.
f- A member function print displays the time in the form hours:minutes:seconds, for example 22:12:13.
g- A member function that adds two time objects
h- Boolean member functions that compare time objects.

4- In order to evaluate your work in a busy day, declare an array of 10 time objects, and initialize it to the times needed to perform your most important 10 tasks during this day (a task cannot exceed 3 hours).
a- Calculate and print the total time needed to perform these tasks.
b- Write a function bubble sort for time objects and use it to sort the array in ascending order of time. Use the result to print the time needed by the most important two tasks.

NEED help in The parts: 3 -h- and 4 -b- ;
my code is :

`````` #include <iostream>
using namespace std;

class Time {

private:

int hours;
int minutes;
int seconds;

public:

Time () {

hours = 0;
minutes = 0;
seconds = 0;

}

Time(int hours , int minutes , int seconds){

switch( seconds < 60 ){

case true:

seconds = seconds;

case false:

seconds = seconds - 60;

minutes++;

if( minutes >59 && hours < 24 ){

seconds = 0;
minutes=0;
hours++;
}
else if ( hours > 24 ){

hours = 0;
seconds = 0;
minutes = 0;
}
}
}

Time ( int seconds ) {
seconds = 0 ;
}

Time ( const Time &t){

*this = t;
}

void set_time( int hrs , int mins , int secs ){

hours = hrs;
minutes = mins;
seconds = secs;

}

int get_hours () {
return hours;
}

int get_minutes () {
return minutes;
}

int get_seconds () {
return seconds;
}

void displayTime (){

cout << "the time in the form hours:minutes:seconds :" << hours << ":" << minutes;
cout <<  ":" << seconds << endl;

}

Time operator +(const Time & t){

Time time1;

time1.hours = t.hours + this->hours;
time1.minutes = t.minutes + this->minutes;
time1.seconds = t.seconds + this->seconds;

return time1;
}

};

int timeinseconds( int hours, int minutes, int seconds){

int s1=0;
int s2=0;
int s3=0;

s1 = hours * 3600 ;
s2 = minutes * 60 ;
s3 = s1 + s2 + seconds ;

return s3;
}

int main() {

int seconds = 0, minutes = 0, hours = 0, seconds_t = 0;

cout << "enter seconds :" << endl;
cin >> seconds_t;

hours   = (seconds_t/3600)%24;

minutes = (seconds_t/60)%60;

seconds = (seconds_t)%60;

cout << " the corresponding time in the form:  hours:minutes:seconds  is : " << hours;
cout << ":" << minutes << ":" << seconds << endl;

Time t;
Time time;

int h=0,m=0,s=0;

cout << "enter the times needed to perform your most important 10 tasks "<<;
cout <<" during the day (hours then minutes then seconds ) : " << endl;

for( int i=0; i<10 ;i++ ){

cin >> h >> m >> s;

if( h <=3 ) {

t[i].set_time(h,m,s);

}

else
return false;

time = time + t[i];

}

time.displayTime();

system( "pause" );
return 0;
}
``````

## All 4 Replies

For 3h you could do something like:

``````Time(const unsigned int hours , const unsigned int minutes , const unsigned int seconds)
: hours(hours), minutes(minutes), seconds(seconds)
{
minutes += seconds / 60;
seconds  = seconds % 60;

hours += minutes / 60;
minutes = minutes % 60;

hours = hours % 24;
}
``````

As far as bubble sort goes, what is your problem with it? There's countless examples of bubblesort on the web and the algorithm itself is easy to comprehend. Maybe you should write a bubble sort function first that sorts an array of integers. After that you can easily modify it to sort an array of "Time" objects.

3-h- should be a boolean function and regarding 4-b- the problem is how to know the importance of a task is it time consuming wise or ....

Errr oops, I somehow read 3b even though I ended up stating 3h in the reply.. I might take a look at 'h' when I have time to do so

With 2 class methods timeinseconds and secondstotime your operators code(+ ==) becomes much easier. This way you should be able to use the == operator to test for equality. You can also use the secondstotime in your constructor to accept only seconds as the parameter.

Something like this might help:

``````    Time operator +(const Time & t){
int tinsecs = timeinseconds(t);
int thisinsecs = timeinseconds();
return secondstotime(tinsecs + thisinsecs);
}
bool operator== (const Time & t)
{
return timeinseconds() == timeinseconds(t);
}
private:
int timeinseconds()
{
return (hours * 3600) + (minutes * 60) + seconds;
}
int timeinseconds(Time t)
{
return (t.hours * 3600) + (t.minutes * 60) + t.seconds;
}

Time secondstotime(int secs)
{
Time temp;
temp.hours = (int)(secs/3600);
secs -= temp.hours * 3600;
temp.minutes = (int)(secs/60);
secs -= temp.minutes * 60;
temp.seconds = secs;
return temp;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.