Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 5, Problem 15C
Write a recursive method that will output all the subsets of a set of n elements (without repeating any subsets).
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a recursive method that for a positive integer returns a string with commas in the appropriate places, for example, putCommas(1234567) returns the string “1,234,567.”
The following recursive method get Number Equal searches the array x of 'n integers
for occurrences of the integer val. It returns the number of integers in x that are
equal to val. For example, if x contains the 9 integers 1, 2, 4, 4, 5, 6, 7, 8, and 9,
then getNumberEqual(x, 9, 4) returns the value 2 because 4 occurs twice in x.
public static int getNumberEqual(int x[], int n, int val) {
if (n< 0) (
return 0;
}
else {
if (x[n-1) == val) {
return getNumberEqual(x, n-1, val) +1;
}
else {
return getNumber Equal(x, n-1, val);
} // end if
) // end if
} // end get Number Equal
Demonstrate that this method is recursive by listing the criteria of a recursive
solution and stating how the method meets each criterion.
1. Write a recursive method expFive(n) to compute y=5^n. For instance, if n is 0, y is 1. If n is 3,
then y is 125. If n is 4, then y is 625. The recursive method cannot have loops. Then write a
testing program to call the recursive method. If you run your program, the results should look like
this:
> run RecExpTest
Enter a number:
3
125
>run RecExpTest
Enter a number:
3125
2. For two integers m and n, their GCD(Greatest Common Divisor) can be computed by a
recursive function. Write a recursive method gcd(m,n) to find their Greatest Common
Divisor. Once m is 0, the function returns n. Once n is 0, the function returns m. If neither
is 0, the function can recursively calculate the Greatest Common Divisor with two
smaller parameters: One is n, the second one is m mod n. Although there are other
approaches to calculate Greatest Common Divisor, please follow the instructions in
this question, otherwise you will not get the credit. Meaning your code needs to follow
the given algorithm.
Then…
Chapter 5 Solutions
Data Structures and Algorithms in Java
Ch. 5 - Prob. 1RCh. 5 - Prob. 2RCh. 5 - Prob. 3RCh. 5 - Prob. 4RCh. 5 - Prob. 5RCh. 5 - Draw the recursion trace for the execution of...Ch. 5 - Prob. 7RCh. 5 - Describe a recursive algorithm for converting a...Ch. 5 - Prob. 9RCh. 5 - Prob. 10R
Ch. 5 - Prob. 11CCh. 5 - Prob. 12CCh. 5 - Give a recursive algorithm to compute the product...Ch. 5 - In Section 5.2 we prove by induction that the...Ch. 5 - Write a recursive method that will output all the...Ch. 5 - In the Towers of Hanoi puzzle, we are given a...Ch. 5 - Write a short recursive Java method that takes a...Ch. 5 - Write a short recursive Java method that...Ch. 5 - Use recursion to write a Java method for...Ch. 5 - Write a short recursive Java method that...Ch. 5 - Prob. 21CCh. 5 - Prob. 22CCh. 5 - Prob. 23CCh. 5 - Isabel has an interesting way of summing up the...Ch. 5 - Prob. 25CCh. 5 - Prob. 26CCh. 5 - Prob. 27PCh. 5 - Write a program for solving summation puzzles by...Ch. 5 - Prob. 29PCh. 5 - Write a program that can solve instances of the...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Distinguish among data definition commands, data manipulation commands, and data control commands.
Modern Database Management (12th Edition)
When only a copy of an argument is passed to a function, it is said to be passed by _______.
Starting Out with C++ from Control Structures to Objects (9th Edition)
The ________ object is assumed to exist and it is not necessary to include it as an object when referring to it...
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
(Catch Parameter) Under what circumstances would you not provide a parameter name when defining the type of the...
C How to Program (8th Edition)
The Python built-in function str () will convert a numerical argument into a character string representation, a...
Computer Science: An Overview (12th Edition)
Explain why problem formulation must follow goal formulation.
Artificial Intelligence: A Modern Approach
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
- Write a recursive method tripleChar(String s, char c) that takes a string s and a character c. The method returns the string s with all the occurrences of c repeated 3 times.arrow_forwardWrite a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem.arrow_forwardWrite a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] Hint:The trick is to remove the last element each time to make the ArrayList a little shorter. import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum =…arrow_forward
- Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] ================================================ import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum = findMinimum(numbers);System.out.println("Minimum: " + minimum);}public static int…arrow_forwardGiven a list of integers, you want to know whether it is possible to divide the integers into two sets, so that the sum of one set is odd, and the sum of the other set is a multiple of 10. Every integer must be in one set or the other. You can write a recursive helper method that takes any number of arguments and then call it inside the method, but you cannot use any loops. Test cases: oddAndTen([5, 5, 3]) true oddAndTen ([5, 5, 4]) oddAndTen ([5, 5, 4, 1]) false truearrow_forwardWrite a recursive method to print all the permutations of astring. For example, for the string abc, the permutation isabcacbbacbcacabcba public static void displayPermutation(String s)public static void displayPermutation(String s1, String s2)The first method simply invokes displayPermutation(" ", s). The secondmethod uses a loop to move a character from s2 to s1 and recursively invokesit with new s1 and s2. The base case is that s2 is empty and prints s1 to theconsole.Write a test program that prompts the user to enter a string and displays all itspermutations.arrow_forward
- Write a recursive method that displays all permutations of a given array ofintegers. Here is a sample run: Enter the array size : 3[1, 2, 3][1, 3, 2][2, 1, 3][2, 3, 1][3, 1, 2][3, 2, 1]arrow_forwardThere is an error in the following recursive method, which implements a binary search. Find the error and indicate how to fix the code so that the method will work correctly.arrow_forwardWrite recursive methods for the following operations: • removeDup(String s): takes string s and returns another string r constructed from s after removing all duplicates. For example, when s = "pineapples", r will be "pineals". • intersection(String s1, String s2): takes two duplicate-free strings and returns the string consisting of all letters that appear in both s1 and s2. • union(String s1, String s2): takes two duplicate-free strings and returns the string consisting of all letters that appear in either s1 or s2. • difference(String s1, String s2): takes two duplicate-free strings and returns the string consisting of all letters that appear only in s1. For example, when s1 = "aples" and s2 = "pears", intersection(s1, s2) will return "apes", union(s1, s2) will "aplesr", and difference(s1, s2) "l". Calling method removeDup() is not allowed in intersection(s1, s2) , union(s1, s2) or difference(s1, s2) method. Your program should check strings are duplicate free before they are…arrow_forward
- JAVA Write a static recursive method evenFactors that takes as input two positive integers and prints the even factors of the first integer that are greater than or equal to the second integer. For example, evenFactors(18,1) prints 2, 6, and 18, since the even factors of 18 that are greater than or equal to 1 are 2,6, and 18.arrow_forwardDo not use static variables to implement recursive methods. USING JAVA: // P6 public static int countSubstrings(String s1, String s2) { } Write a recursive method countSubstrings that counts the number of non-overlapping occurrences of a string s2 in a string s1. Use the method indexOf() from String class. As an example, with s1=”abab” and s2=”ab”, countSubstrings returns 2. With s1=”aaabbaa” and s2=”aa”, countSubstrings returns 2. With s1=”aabbaa” and s2=”AA”, countSubStrings returns 0. Show the output on the following strings: s1 = “Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.…arrow_forwardJava - Write a method that uses a loop to figure out the factorial (n!). The method will take an integer n and return the value of n! (return answer, don’t print) Take the method above and convert it to a recursive method.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7); Author: CS Dojo;https://www.youtube.com/watch?v=D6xkbGLQesk;License: Standard YouTube License, CC-BY