45.8k views
4 votes
In function main prompt the user for a time in seconds. Call a user defined function to calculate the equivalent time in hours, minutes, and seconds. Parameters should be the time in total seconds and pointers to the hours, minutes, and seconds. Print the equivalent in hours, minutes, and seconds in function main. Test with a value of 36884 seconds Output should look something like this: 5000 seconds can be broken into 1 hour 23 minutes and 20 seconds"

User Morsanu
by
8.7k points

1 Answer

4 votes

Answer:

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

cout<<"Enter time in seconds:";

int time, hour, minutes, seconds,initialTime;

cin>>time;

initialTime = time;

hour = floor(time / 3600);

time = time % 3600;

minutes = floor(time / 60);

time = time % 60;

seconds = time;

cout<< initialTime<<" can be broken down into: "<<endl;

cout<<hour << " hour(s)"<<endl;

cout<<minutes <<" minutes"<<endl;

cout<<seconds <<" seconds"<<endl;

return 0;

}

Step-by-step explanation:

The programming language use is c++

The module cmath was called to allow me perform some math operations like floor division, and the Iostream allows me to print output to the screen. Using namespace std allows me to use classes from the function std.

I prompt the user to enter time in second, then declare all the variables that will be used which include time, hour, minutes, seconds and initialTime.

initialTime is used to hold the the time input entered by the user and will be printed at the end no arithmetic operation is carried out on it.

Floor division (returns the quotient in a division operation) and Modulo (returns the remainder in a division operation) division is used to evaluate the hours, the minutes and the seconds.

The final answers are then printed to the screen.

I have uploaded the c++ file and a picture of the code in action

In function main prompt the user for a time in seconds. Call a user defined function-example-1
User Siggen
by
7.9k points