For each of the following situations, tell which type of loop (while , do-while, or for) would work best:
a. Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/10.
b. Reading in the list of exam scores for one student.
c. Reading in the number of days of sick leave taken by employees in a department.
d. Testing a function to see how it performs for different values of its arguments.
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Problem Solving with C++ (9th Edition)
Additional Engineering Textbook Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
C++ How to Program (10th Edition)
Starting Out with Python (3rd Edition)
Starting Out with Programming Logic and Design (4th Edition)
- Write a program in Python 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.arrow_forwardWrite a program in python 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. Enemy was hit!Enemy…arrow_forwardWrite function that is given in question. Do correct.arrow_forward
- Write and test the “digit” function:Function Prototype: int digit(int n,int k)This function returns the kth digit of the positive integer n. For example, if n is the integer 29,415, then the call digit(n,0) would return the digit 2, and the call digit(n,2) would return the digit 4.arrow_forwardThis is for R Regarding a function, which of the following is not true? A function must contain a return statement. When a function is defined, it must have parentheses. A function can contain a for loop or an if statement. The function keyword is used to create a function.arrow_forwarda. Implement the problem above using C/C++ programming language. Do apply the techniques for good programming practices. Ensure that appropriate validations are done on your input data. b. Sample Input: The user should be allowed to input the following information:i. Maximum weight a truck is allowed to carryii. Number of oversized items to ship for the dayiii. Weight of each oversized itemiv. Priority status for oversized item (if applicable) Sample Output:i. Oversized items that were loaded into each truckii. Amount of unused space (weight) for each truckiii. Number of trucks that were not utilized/loaded (if any)iv. List of oversized items that could not be shipped (including priority status, if applicable)arrow_forward
- Decryption Key A|B|C| D|E|F|G|H|I|J|K|L|M N|O|P|Q|R|S|T|U|V|W|X|Y|Z (letter above equals below, and vice versa) As you can see, A becomes N, B becomes O and so on. Your job is to write a program, with at least four functions, including main, which must do the following: 1. Ask user whether they want to encode or decode a message - if no, then terminate 2. Take the input string from the user, store it in dynamic memory (use new) 3. As appropriate, encode or decode the message using Rot13. 4. Output the encoded/decoded message 5. Delete the input string from dynamic memory (use delete)arrow_forwardMatch the following function with a possible asymptotic notation. I've tried to start it, but I need a little help. Thank youarrow_forwardExercise 3: Write a function fact (n) which returns the factorial of n (n!). Note: n! is undefined for n<0 so just have the function return 0 for n<0.0! Is defined to be equal to 1. Use a loop.arrow_forward
- Write a function that does the following for the given values: add, subtract, divide and multiply. These are simply referred to as the basic arithmetic operations. The variables have to be defined, but in this challenge they will be defined for you. All you have to do is check the variables, do some string to integer conversions, use some if conditions, and apply the arithmetic operation. Examples operation ("1", "2", operation ("4", "5", operation ("6", "3", "add" ) → 3 "subtract") → -1 "divide") → 2arrow_forward1. Write a program uses a Boolean-valued function to determine whether a set of numbers is in a given range. a) Define a function, isInRange(x) with an integer parameter. b) isInRange(x) returns true if the integer number x is in the range (-100, 100). Otherwise, return false.c) Define a main() function, in which a loop will be created to do the following ten times: 1) get a random integer, n, in the range (-500, 500). 2) call isInRange(x) function to determine and print out if the number n is in the range. d) Call main() function to initiate the tasks to be performed. Output Example: 2 is in the range.66 is in the range.171 is not in the range. 67 is in the range.-28 is in the range.355 is not in the range. 343 is not in the range. 233 is not in the range. -263 is not in the range. 11 is in the range. 2. Write a program that sums up all positive user input numbers. a) For every iteration output the sum of all positive input values.b) Implement the loop to stop if a negative…arrow_forwardPython: we usually refer to two floating point numbers as being equal when they are close enough in value for our purposes. Write a function named almost_equal that receives two floating point numbers and returns whether they are close enough to each other. Two floating points numbers are close enough to each other if they are within one-tenth of 1% (0.001). Write a main() function in your file to exercise your function to show that it works correctly. Your test implementation should not rely on visual inspection to determine if the tests pass. Hint: The fabs function in the math module of Python's libraries returns the absolute value of its parameter.arrow_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