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 take 5 digit number as an input from the user and perform the following tasks:1. Split a number into digits.2. Find the sum of the all the digits in the number.3. Find the product of the all the digits in the number.4. Find maximum number with in the number along with position.5. Printing the digits in the reverse order.Note: Make user defined functions to perform each task.
Write a program that reads a list of names and total points for students until -1 is entered. After gettingall of names and points, it will output them as names and grades. Write your program using a functionthat gets the points as parameter and calculates the grade and return it. No input validation is required.Also write a function that unit test your grade calculator function.Bonus (5 points): validate your input in any way that you think works better for this situation.Show an error messageBring the points to a valid rangeReturn a value that shows an error has occurredA[94,100] A-[90,94) B+[87,90) B[84,87) B-[80,84) C+[77,80) C(74,77) C-[70,74) D+[67,70) D[61,67) F[0,61)Sample input:Lee 39Lua 86Mary 91Stu 72-1Sample output:Lee FLua BMary AStu C-
Write a program that inputs a line of text and a search string from the keyboard. Using function strstr, locate the first occurrence of the search string in the line of text, and assign the location to variable searchPtr of type char *. If the search string is found, print the remainder of the line of text beginning with the search string. Then, use strstr again to locate the next occurrence of the search string in the line of text. If a second occurrence is found, print the remainder of the line of text beginning with the second occurrence. input Input a line of text and a search string. Maximum number of char is 200. Output Print the remainder of the line of text beginning with the search string.

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