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
Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
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