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 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: ......
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 reads in two numbers and stores them into two variables a and b respectively. These values should then be passed into a function that swaps the values stored in the two variables (hint: use a temporary local variable). The values of the two variables should then be printed out (but not in the function where they are swapped). Note: you should use separate functions for each of the major steps in this program (i.e. input, processing, output). Remember, if you wish to pass two values out of a function, you will need to use pass by pointer parameters.

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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
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