Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 14.1, Problem 5STE
Program Plan Intro
Recursive function:
- Recursive function is a function that call itself.
- A function definition may have a call to the function being defined. In such cases, the function is known as recursive.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a recursive void function that has one parameter that is a positive integer. When called, the function writes its argument to the screen backward. That is,, if the argument is 1234, it outputs the following to the screen: 4321.
I've literally tried to create this program and I have no idea how to make it work. Please help me. Is it possible to explain line by line what the code does?
Write an iterative version of the function defined in Self-Test Exercise 2.
(Write a recursive void function that has one parameter that is a positive integer and that writes out that number of asterisks (*) to the screen, all on one line.)
Write a recursive function called draw_triangle() that outputs lines of '*' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting.
Hint: The number of '*' increases by 2 for every line drawn.
Ex: If the input of the program is:
3
the function draw_triangle() outputs:
* ***
Ex: If the input of the program is:
19
the function draw_triangle() outputs:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Note: No space is output before the first '*' on the last line when the base length is 19.
if __name__ == '__main__': base_length = int(input()) draw_triangle(base_length)
Chapter 14 Solutions
Problem Solving with C++ (10th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Similar questions
- Need python help. For problems 1 and 2, add your code to the file Lab2.java. Add your tests in the main function of Lab2.java. Do not use static variables in class Lab2 to implement recursive methods. Problem 1: Implement a recursive method min that accepts an array and returns the minimum element in the array. The recursive step should divide the array into two halves and find the minimum in each half. The program should run in O(n) time and consume O(logn) memory. Demonstrate the output of min on the array int [] a = { 2, 3, 5, 7, 11, 13, 17, 19, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 23, 29, 31, 37, 41, 43 } Problem 2 You have been offered a job that pays as follows: On the first day, you are paid 1 cent, on the second day, 2 cents, on the third day, 4 cents and so on. In other words, your pay doubles every day. Write a recursive method computePay that for a given day number computes the pay in cents. Assume that you accumulate all the money that you are paid. Write a…arrow_forwardHi can check my code below and see whether it has meet the following, if not kindly assist and correct it for me. a). Recursive function in Java that accepts an integer as input and returns 1 + 1/2 + 3 + 1/4 + ...(n or 1/n). b). The answer is the sum of the odd integers from 1 to n plus the sum of the reciprocals of the even integers. Thank you. public class Main {public static double sum(int n) {if (n <= 0) {return 0;} else if (n % 2 == 0) {return 1.0 / n + sum(n - 1);} else {return n + sum(n - 1);}}public static void main(String[] args) {System.out.println(sum(5));}}arrow_forwardint p =5 , q =6; void foo ( int b , int c ) { b = 2 * c ; p = p + c ; c = 1 + p ; q = q * 2; print ( b + c ); } main () { foo (p , q ); print p , q ; } Explain and print the output of the above code when the parameters to the foo function are passed by value. Explain and print the output of the above code when the parameters to the foo function are passed by reference. Explain and print the output of the above code when the parameters to the foo function are passed by value result. Explain and print the output of the above code when the parameters to the foo function are passed by name.arrow_forward
- Implement a recursive function called evens that returns an integer with only theeven numbers. Note this function is returning an integer, not printing. There shouldbe no use of cout within your function. The function declaration should look asfollows:int evens(int n);(ex. evens(234567); returns 246)(ex. cout << evens(56032); prints 602)arrow_forwardimplement pass-by-value parameter passing method.USE C LANGUAGEarrow_forwardNo plagarism! Correct and detailed answer ( code with comments and output screenshot if necessary)arrow_forward
- don't want answer from chat gpt.arrow_forwardCould you help me the following question Below is a recursive function written in Java that takes 2 integers and returns an integer: public static int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x/y); } Answer the following multiple choice questions. Remember that multiple answers can still apply. Based on how this code is exactly written, if we called this function with “gcd(25, 15);” from main, which of the following is true: In the first function instance of gcd, x will be 25 and y will be 15. This will not trigger the base case so a recursive function call to gcd will then occur When the base case is finally reached, y will be 0 and x will be 5 We will be in the 4th function instance of gcd when the base case is reached None of the above If we changed the code in the else block to “return gcd(y, x%y);”, which of the following would then be true: If we call “gcd(5,5);”, we will get an infinite loop If…arrow_forwardcreate a function named powerHalf(x, n), where this function is recursive like power(x, n), but it also uses the idea that xn = (x n/2)2 when n is even. Use the global variable countcalls to verify that this version of the power function is more efficient than power(x,n), where n is 0 or a positive integer with the base case that: xn = x * xn-1 use python pleasearrow_forward
- Write in C++ Alice is trying to monitor how much time she spends studying per week. She going through her logs, and wants to figure out which week she studied the least, her total time spent studying, and her average time spent studying per week. To help Alice work towards this goal, write three functions: min(), total(), and average(). All three functions take two parameters: an array of doubles and the number of elements in the array. Then, they make the following computations: min() - returns the minimum value in the array sum() - returns the sum of all the values in the array average() - returns the average of all the values in the array You may assume that the array will be non-empty. Function specifications: Function 1: Finding the minimum hours studied Name: min() Parameters (Your function should accept these parameters IN THIS ORDER): arr double: The input array containing Alice's study hours per week arr_size int: The number of elements stored in the array Return Value:…arrow_forwardImplement the Following Functions➔ void get_input(int& input1, int& input2)Reads two integers from the keyboard and stores them in input1 and input2respectively.➔ void swap_values(int& variable1, int& variable2)swaps the values of variable1 and variable2.➔ void order(int& n1, int& n2)Orders the numbers in the variables n1 and n2 so that after the function calln1 <= n2 i.e(swaps the n1 and n2 only if n1<=n2 using swap_values function)➔ void give_results(int output1, int output2)Outputs the values in output1 and output2. Assumes that output1 <= output2. write a C++ programarrow_forward10arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning