Absolute C++, Student Value Edition, 6/e
Absolute C++, Student Value Edition, 6/e
6th Edition
ISBN: 9780133970982
Author: SAVITCH
Publisher: Pearson Education
bartleby

Videos

Textbook Question
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 reads a sequence of input values and displays a bar chart of the values, using asterisks, like this: ********************** **************************************** **************************** ************************** ************** You may assume that all values are positive. You will keep reading input until the letter Q is entered. In order to solve this problem, you need to first figure out the maximum entered value. That value’s bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks. Make sure that you use functions in your solution, including a main function. Define def readInts() function to read integers until user enters q. and returns a list of entered integers Define def printStars(values) function that prints the bar chart using the list of values
Write a program that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical. Ex: If the input is 93, the output is: 93 92 91 90 89 88 Ex: If the input is 77, the output is: 77 Ex: If the input is not between 20 and 98 (inclusive), the output is: Input must be 20-98 For coding simplicity, follow each output number by a space, even the last one. Use a while loop. Compare the digits; do not write a large if-else for all possible same-digit numbers (11, 22, 33, ..., 88), as that approach would be cumbersome for large ranges.
Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n Nobody the output is: 0 Your program must define and call the following function that returns the number of times the input character appears in the input string. int CountCharacters(char userChar, string userString) Note: This is a lab from a previous chapter that now requires the use of a function. #include <iostream>using namespace std; /* Define your function here */ int main() {/* Type your code here. Your code must call the function. */ return 0;} Please help me with this problem using c++.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
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
  • Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the last name. Hint: Use the to_string() function to convert numerical data to a string.
    Write a program which will take marks of three quizzes as input, then call a function void ScaleUp(float q1, float q2, float q3) to check if the marks of any of the quiz ‘q’ is less than the average of rest of two quizzes ‘a’. If marks of any quiz is less than the average of the rest of two marks then add the difference between the average of the two quizzes and the quiz itself to q i.e., q = a - q, to scale up the quiz marks.For example, if q1 = 5, q2 = 8 and q3 = 9 then your function will change the marks of quiz 1 using the steps below:A = (q3 + q2 )/2 => (8+9)/2=> 8.5 Diff = A - q1 => 8.5 - 5 => 3.5 Q1 = Q1 + Diff => 5 + 3.5 => 8.5Note that the marks are input from the user, so there might be three cases I. if q1 < q2 + q3 orIi. if q2 < q1 + q3 orIii. if q3 < q1 + q2
    Write a program, which takes a positive integer as input, and prints which powers of 2 does the numberlie between. For example, the number 269 lies between 28(256) and 29(512). If the input is 269, theoutput should be 8 9. Borderline cases which are powers of 2, such as 256, should be aligned to the lowerlimit of the desired range output, i.e. 28(256). You should not use math functions such as pow.
    • SEE MORE QUESTIONS
    Recommended textbooks for you
  • C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
  • 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