Exercise 3 Filename: seconds.cpp 1. Write a function called NumOfSeconds that ta
ID: 3902601 • Letter: E
Question
Exercise 3
Filename: seconds.cpp
1. Write a function called NumOfSeconds that takes in three integer parameters (representing hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12" (i.e. was at 12:00:00 -- AM or PM doesn't matter since you're not tracking this). NO user input or output to the screen should occur in this function.
2. To test this function, write a main() routine (in the same file) that prompts the user to enter hours, minutes, and seconds for two different clock times; then uses the Seconds function to calculate the shortest amount of time in seconds between the two times (both of which are within one 12-hour cycle of the clock); the print out the number of seconds since "striking 12" for both clocks, as well as the number of seconds between the two clock times
Sample Run 1:
(user input underlined)
Sample Run 2:
(user input underlined)
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
using namespace std;
//returns the number of seconds since the last time the clock "struck 12"
//assuming all inputs are valid and in 12 hour format
int NumOfSeconds(int hours,int mins, int secs){
//finding the hour difference from 12
int hoursDifference;
if(hours<12){
//'hours' number of hours passed since 12
hoursDifference=hours;
}else{
//its currently 12 hours
hoursDifference=0;
}
//finding the total seconds
int totalSeconds=hoursDifference*60*60;//converting hours to seconds
totalSeconds+=mins*60;//appending remaining minutes to seconds
totalSeconds+=secs; //appending remaining seconds
return totalSeconds;
}
int main(){
int hours1, min1, sec1;
int hours2, min2, sec2;
//getting input
cout<<"Input first clock time..."<<endl;
cout<<"Hours: ";
cin>>hours1;
cout<<"Minutes: ";
cin>>min1;
cout<<"Seconds: ";
cin>>sec1;
cout<<"Input second clock time..."<<endl;
cout<<"Hours: ";
cin>>hours2;
cout<<"Minutes: ";
cin>>min2;
cout<<"Seconds: ";
cin>>sec2;
//finding seconds ellapsed since 12
int totalSeconds1=NumOfSeconds(hours1,min1,sec1);
int totalSeconds2=NumOfSeconds(hours2,min2,sec2);
//displaying it
cout<<"There have been "<<totalSeconds1<<" seconds since the 1st clock hit 12:00"<<endl;
cout<<"There have been "<<totalSeconds2<<" seconds since the 2nd clock hit 12:00"<<endl;
int difference;
//finding the difference
if(totalSeconds1>totalSeconds2){
difference=totalSeconds1-totalSeconds2;
}else{
difference=totalSeconds2-totalSeconds1;
}
cout<<"The two times are "<<difference<<" seconds apart"<<endl;
return 0;
}
/*OUTPUT*/
Input first clock time...
Hours: 6
Minutes: 45
Seconds: 30
Input second clock time...
Hours: 4
Minutes: 50
Seconds: 12
There have been 24330 seconds since the 1st clock hit 12:00
There have been 17412 seconds since the 2nd clock hit 12:00
The two times are 6918 seconds apart
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.