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
C++ How to Program (10th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Objects (6th Edition)
C Programming Language
Software Engineering (10th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- 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 with python using random module for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function until you run out of ammo, at which point you lose. Report both hits and misses (see Sample Outputs). If your shot is a hit as determined by the value returned by shoot, your code should lower the enemy's health. If you are lucky, the health of the enemy will be reduced to zero before you run out of ammo. If this happens, report the enemy's demise and use the break keyword to stop the loop. You have won.Sample Output 1Shot fired. Enemy was hit!Shot fired. Shot missedShot fired. Enemy was hit!Shot fired. Enemy was hit!Shot fired. Enemy was hit!Shot fired.…arrow_forwardWrite 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.arrow_forward
- 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.arrow_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_forwardWrite 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.arrow_forward
- The ceiling of a floating-point number x is the smallest integer that is still larger than or equal to x. Alternatively, the ceiling of a floating-point number x is what you get when you round x up to the nearest integer. For example, the ceiling of 2.1 is 3, the ceiling of 0.9 is 1, the ceiling of -4.5 is -4, etc. Write a function called ceiling() to compute the ceiling of a float input parameter that returns one integer value. You may not use python’s ceil() or floor() functions. Your function may use int()/float() functions, and the floor division operator (i.e., '//').arrow_forwardThis is python programming Create a function based on the following information: You are given two strings with words separated by commas. Try to find what is common between these strings. The words in the same string don't repeat. Your function should find all of the words that appear in both strings. The result must be represented as a string of words separated by commas in alphabetic order. Input: Two arguments as strings. Output: The common words as a string. Return: Nothing is returnedarrow_forward6. Write a program which reads string and rewrite it in alphabetical order. For example, the word STRING should be written as GINRST. 7. Write a program to replace a particular word by another word in a given string. For example the “PYTHON” should be replaced by “C” in the text “It is good to program in PYTHON language”. 8. Write a program that will find the maximum number from 3 float type numbers. You have to usethe function getMax which will take 3 pointer variables and return the maximum number.arrow_forward
- write a python program 9. You are a class teacher at a kindergarten school. As a task, you asked your students to write a paragraph. Unfortunately, you notice that most of the students did not use capital letters correctly. Your task is to write a function which takes a string as its only parameter and returns a new copy of the string that has been correctly capitalized. Your function should: Capitalize the first letter in the string Capitalize the first letter after a full-stop, exclamation mark or question mark • Capitalize the word “i" if it is in lowercase. Summary: You have to write a function that reads a string from the user and capitalizes. The string is then returned and displayed. Sample Input ('my favourite animal is a dog. a dog has sharp teeth so that it can eat flesh very easily. do you know my pet dog's name? i love my pet very much.') Sample Output My favourite animal is a dog. A dog has sharp teeth so that it can eat flesh very easily. Do you know my pet dog's name? I…arrow_forwardWrite a simple calculator program with four operations: add, subtract, multiply, and divide. Create a separate function for each that returns the result of the operation (for example, an “add” function that returns the sum of two values). In the main() function, the program should ask the user to enter two numbers (floating point type) separated by a '+', '-', '*', or '/' character. The program should then call the appropriate function and display the result of the operation.arrow_forwardWrite a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. If the input is 7 15 3, the output is: largest: 15 smallest: 3 Your program should define and call two functions: Function LargestNumber(integer num1, integer num2, integer num3) returns integer largestNumFunction SmallestNumber(integer num1, integer num2, integer num3) returns integer smallestNumarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education