Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 1PP

Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. There should be at least three functions: one for input, one to do the conversion, and one for output. Record the A.M./P.M. information as a value of type char, 'A' for A.M. and ' P' for P.M. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is A.M. or P.M. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.

Expert Solution & Answer
Check Mark
Program Plan Intro

List of variables:

  • hours: Store the time in hours.
  • minutes: Store the time in minutes.
  • answer: Store the value response ‘y’ or ‘n’.
  • result: To store the values of ‘A’ or ‘P’ for AM or PM.

List of functions used:

  • getTime(): Used to take values of hours and minutes using call by reference parameters.
  • output(): To show the output for time.
  • conversion(): To convert the time either in AM or PM.
  • cin(): To take input from input streams like keyboard, files etc.
  • cout(): To display the output.

Summary Introduction:

Program will use the Main () method to prompt the user to enter the time in 24-hour notation and convert it in 12-hour notation. For this, three functions are provided: one for input values, second for conversion and third for output values.

Program Description:

The purpose of the program is to convert the time from 24-hour notation to 12-hour notation.

Explanation of Solution

Program:

Following is the C++ program to convert the time from 24-hour notation to 12-hour notation.

#include <iostream>
using namespace std;
char conversion(int& hours);
void getTime(int& hours, int& minutes);
void output(int hours, int minutes, char result);
int main(){
 int hours, minutes;
 char answer;
do{
getTime(hours, minutes);
 char cp = conversion(hours);
output(hours, minutes, cp);
cout<< "\nDo you want to calculate for more time? ";
cin>> answer;
}while(answer == 'Y' || answer == 'y');
cout<< "\nThank you! ";
 return 0;
}
void getTime(int& hours, int& minutes){
 //Enter the hours and minutes as call-by-reference parameters
cout<< "Enter the time in hours: ";
cin>> hours;
cout<< "Enter the time in minutes: ";
cin>> minutes;
}
char conversion(int& hours){
 char result;
 int division;
if(hours <= 12){
 result = 'A';
 }
else{
 division = hours % 12;
if(division == 0){
 hours = 00;
 }
else{
 hours = division;
 }
 result = 'P';
 }
 return result;
}
void output(int hours, int minutes, char result){
cout<< "The time is: " << hours<< ":" << minutes << " " << result << "M";
cout<<endl;
}

Explanation:

In the above program, under main function, getTime() function which has two parameters. Hours and minutes is called to enter the values of hours and minutes. After that, conversion() function which has one parameter is called and output is stored in the variable cp. Finally, the output() function is used to display the result.

Sample Output:

Enter the time in hours: 15 
Enter the time in minutes: 24 
The time is: 3:24 PM 
Do you want to calculate for more time? y 
Enter the time in hours: 11 
Enter the time in minutes: 39 
The time is: 11:39 AM 
Do you want to calculate for more time? n 
Thank you!

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program in Python for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function until you run out of ammo, at which point you lose. Report both hits and misses (see Sample Outputs). If your shot is a hit as determined by the value returned by shoot, your code should lower the enemy's health. If you are lucky, the health of the enemy will be reduced to zero before you run out of ammo. If this happens, report the enemy's demise and use the break keyword to stop the loop. You have won.
Write a program that works with fractions. Your program should be able to add,subtract, multiply, and divide two fractions. Write a separate function for addition,subtraction, multiplication and division. Specifically, your program must request two fractions from the user, getting the numerator and denominator separately for each fraction, and the operation to perform (add, subtract, multiply, or divide).Your program will then compute the resulting fraction, keeping the numerator and denominator separate, and output the result.
write a program that asks the users to input three numbers (x,y,z) and calculate the sum, subtraction, multiplication, and division of the numbers (x/y)/z using functions. There is also a function to print the results.  note: inputMe, sumMe, subtractMe, multipleMe, DivideMe, ResultMe are the names of the functions.  The results should be the same as: The result of sum is: ..... The result of subtract is: .... The result of multiply is: ..... the result of divide is: ......

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Computer Programming for Beginners | Functions, Parameters & Arguments | Ep24; Author: Programming With Avelx;https://www.youtube.com/watch?v=VXlh-qJpfw0;License: Standard YouTube License, CC-BY