Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 12, Problem 2PE
Program Plan Intro
Recursive Multiplication
Program Plan:
- • Define the “main()” function:
- ○ Initializes the variable “n1” as “0”
- ○ Initializes the variable “n2” as “0”
- ○ Check the value of “n1”.
- ■ If it is less than or equal to “0”, then get the first number from the user and store it to the variable “n1”.
- ○ Check the value of “n2”.
- ■ If it is less than or equal to “0”, then get the second number from the user and store it to the variable “n2”.
- ○ Call the function “multiplyRecursive()” and pass the two arguments “n1” and “n2”.
- • Define the “multiplyRecursive()” function:
- ○ Check the value of “x” and “y”
- ■ If it is equal to “0”, then return “0”.
- ■ Otherwise, call the function “multiplyRecursive()” to recursively perform the addition operation.
- ○ Check the value of “x” and “y”
- • Call the “main()” function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Exponent
y Catherine Arellano
mplement a recursive function that returns
he exponent given the base and the result.
for example, if the base is 2 and the result is
3, then the output should be 3 because the
exponent needed for 2 to become 8 is 3 (i.e.
23 = 8)
nstructions:
1. In the code editor, you are provided
with a main() function that asks the
user for two integer inputs:
1. The first integer is the base
2. The second integer is the result
2. Furthermore, you are provided with the
getExponent() function. The details of
this function are the following:
1. Return type - int
2. Name - getExponent
3. Parameters
1. int - base
2. int - result
4. Description - this recursive function
returns the exponent
5. Your task is to add the base case and
the general case so it will work
Score: 0/5
Overview
1080
main.c
exponent.h
1 #include
2 #include "exponent.h"
3 int main(void) {
4
int base, result;
5
6
printf("Enter the base: ");
scanf("%d", &base);
7
8
9
printf("Enter the result: ");…
Recursive Power FunctionWrite a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program.
SAMPLE RUN #0: ./recursiveExponent
Hide Invisibles
Highlight: Show Highlighted Only
2^3=8↵ 2^4=16↵ 3^3=27↵ 6^3=216↵ 7^7=823543↵ 10^9=1000000000↵
Python program (recursive function)
A recursive function is a function defined in terms of itself via self-referential expressions. This means that the function will continue to call itself and repeat its behavior until some condition is met to return a result.
Write a python recursive functionprod that takes x as an argument, and returns the result where, result=1*1/2*1/3*….*1/n
Include a screenshot that shows a python program that uses the above function and prints the rounded result to three decimal placesafter prompting the user to enter a number, x. Use x=3.
N.B: The code should be included please.
Chapter 12 Solutions
Starting Out with Python (4th Edition)
Ch. 12.2 - It is said that a recursive algorithm has more...Ch. 12.2 - Prob. 2CPCh. 12.2 - What is a recursive case?Ch. 12.2 - What causes a recursive algorithm to stop calling...Ch. 12.2 - What is direct recursion? What is indirect...Ch. 12 - Prob. 1MCCh. 12 - A function is called once from a program's main...Ch. 12 - Prob. 3MCCh. 12 - Prob. 4MCCh. 12 - Prob. 5MC
Ch. 12 - Prob. 6MCCh. 12 - Any problem that can be solved recursively can...Ch. 12 - Actions taken by the computer when a function is...Ch. 12 - A recursive algorithm must _______ in the...Ch. 12 - A recursive algorithm must ______ in the base...Ch. 12 - An algorithm that uses a loop will usually run...Ch. 12 - Some problems can be solved through recursion...Ch. 12 - It is not necessary to have a base case in all...Ch. 12 - In the base case, a recursive method calls itself...Ch. 12 - In Program 12-2 , presented earlier in this...Ch. 12 - In this chapter, the rules given for calculating...Ch. 12 - Is recursion ever required to solve a problem?...Ch. 12 - When recursion is used to solve a problem, why...Ch. 12 - How is a problem usually reduced with a recursive...Ch. 12 - What will the following program display? def...Ch. 12 - Prob. 2AWCh. 12 - The following function uses a loop. Rewrite it as...Ch. 12 - Prob. 1PECh. 12 - Prob. 2PECh. 12 - Prob. 3PECh. 12 - Largest List Item Design a function that accepts a...Ch. 12 - Recursive List Sum Design a function that accepts...Ch. 12 - Prob. 6PECh. 12 - Prob. 7PECh. 12 - Ackermann's Function Ackermann's Function is a...
Knowledge Booster
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
- (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forward2. Sum: a recursive function that computes the sum of integers 1, 2, 3, …., n for a given number n. So Sum(6) should return 1 + 2 + 3 + 4 + 5 + 6 , i.e. 21.sum(n) = n + sum(n-1)arrow_forwardMULTIPLE FUNCTIONS AND RECURSIVE FUNCTIONS.arrow_forward
- MULTIPLE FUNCTIONS AND RECURSIVE FUNCTIONS HANDS-ON use #include<stdio.h>arrow_forwardRecursive functions are ones that repeat themselves repeatedly.arrow_forwardFor glass box testing of a recursive function, you should test cases where: a) the function returns without a recursive call, ie using a base case b) the function makes exactly one recursive call c) the function makes more than one recursive call d) all of thesearrow_forward
- C PROGRAMarrow_forward1.Show the valid base case statements. 2.Show the valid general case statements. 3.Based on the recursive function produce a snippet of non-recursive code that will behave the same with the recursive (e.g. using loop). 4.Consider the following recursive functions: int func(int x) { if (x == 0) return 2; else if ( x == 1 ) return 3; else return (func(x - 1) + func(x - 2) ); } 4i.cout<<func(O)<<endl; 4ii.cout<<func(l)<<endl; 4iii. cout<<func (2) <<endl;4iv. cout<<func (5) <<endl;arrow_forwardPYTHON RECURSIVE FUNCTION Write a python program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia then the output is (must match the below ordering): Julia Lucas Mia Julia Mia Lucas Lucas Julia Mia Lucas Mia Julia Mia Julia Lucas Mia Lucas Juliaarrow_forward
- A recursive function must have a to end the recursion. recursive call base case O value boolean conditionarrow_forward4. CodeW. X b For fun X Solved x b Answer x+ Ohttps://codeworko... CodeWorkout X264: Recursion Programming Exercise: Multiply For function multiply,write the missing base case condition and action. This function will multiply two numbers x and y.You can assume that both x and y are positive. Examples: multiply(2, 3) -> 6 Your Answer: 1 public int multiply(int x, int y) { 2. if > { > } else { return multiply(x 1, y) + y; 3. 5. { 7. 1:08 AM 50°F Clear 日arrow_forwardRecursive Sum! Write a recursive function rc_sum(n:int) -> int that returns the sum of the first n positive integers. The function should look very similar to the factorial function you have seen before. Your Answer: 1 # Put your answer here 2 Submitarrow_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 LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr