Write a
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?
Chapter 4 Solutions
Absolute C++
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
SURVEY OF OPERATING SYSTEMS
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with Python (4th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 the output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order.def swap_values(user_val1, user_val2) I get all tests passed except one.: the one in the bold. It puts parentheses around the answer instead of what they want for output. 0 / 2 Output differs. See highlights below. Input 4 5 Your output (5, 4) Expected output 5 4 2: Unit testkeyboard_arrow_up 2 / 2 swap_values(-1, 10) Your output swap_values(-1, 10) correctly returned 10 -1 3: Unit testkeyboard_arrow_up 3 / 3 swap_values(9, 0) Your output swap_values(9, 0) correctly returned 0 9 4: Unit testkeyboard_arrow_up 3 / 3 swap_values(11, 11) Your output swap_values(11, 11) correctly returned 11 11arrow_forwardWrite 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-arrow_forwardprogram with a function. program with a function.arrow_forward
- Write a program creating a random number between 1 and 500. Assign the random number both to “A” and “B”( A is the max number and B is the minimium number). 2 Write a loop using a function that creates 9 random integers ranging from 1 to 500. 3 Create a function comparing the 9 numbers and determine which one is the largest and which one is the smallest. Comparing with A and B. Print the largest and smallest number. Using c++arrow_forwardWrite a function that finds and returns the smallest value of n for which: 1 + 2 + 3 + ... + n is greater than or equal to max (where max is a number input from the user). You can use the following prototype: int SmallestValue(int); In the main program be sure to declare all variables and show the call. Be user friendly! Sample run: What is the max number: 23 The smallest value of n where the sum of 1 to n is greater or equal to 23 is: 7 Again? Y What is the max number: 12 The smallest value of n where the sum of 1 to n is greater or equal to 12 is: 5 Again? N in c++ please use basic coding I'm not too advancedarrow_forwardA company wants to transmit data over the telephone but is concerned that its phones may b tapped. It has asked you to write a program that will encrypt the data so that it may be transmitted more securely. All the data transmitted is 4 digit integers. Your program should read a four digit integer entered by the user and calls a function encrypt which takes four digits as arguments and encrypt it as follows . Replace each digit with the result of adding 4 to the digit and getting the remainder after dividing the new value by 10. . Calls another function swap which swaps the first digit with the third, and second digit with the fourth using pass by reference. Then it prints the encrypted integer.arrow_forward
- Please solve the question by using MATHEMATICA, Write a function to randomly mix up the characters in a word. For example, if the input is "homework", you might output "eoomkwhr" or "wrhomoke". Your function should work on any input length, and all of the input letters must appear in the output.arrow_forwardUsing python in jupyterlabarrow_forwardWrite a program that will read in a length in feet and inches and output the equivalent length in meters and centimeters. Use at least three functions: one for input, one or more for calculating (consider one to do the opposite conversion), and one for output. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. There are 0.3048 meters in a foot, 100 centimeters in a meter, and 12 inches in a foot.arrow_forward
- Please code this in python according to the function given below.arrow_forwardPlease code this in Python according to the function given below.arrow_forwardWrite a function that displays at the left margin of the screen a solid square out of whatever character is contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacter is #, then this function should print the following: (example of function: void square( int side, char fillCharacter )) Sample output Enter a character and the side length: # 5 ##### ##### ##### ##### #####arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning