Write a recursive method that will compute cumulative sums in an array. To find the cumulative sums, add to each value in the array live sum of the values that precede it in the array. For example, it the values in the array are [2, 3, 1, 5, 6, 2, 7], the result will be [2, (2) + 3, (2 + 3) + 1, (2 + 3 + 1) + 5, (2 + 3 + 1 + 5) + 6, (2 + 3 + 1 + 5 + 6) + 2, (2 + 3 + 1 + 5 +6 + 2) + 7] or [2, 5, 6, 11, 17, 19, 26]. (Hint: The parenthesized sums in the previous example are the results of a recursive call.)
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Database Concepts (7th Edition)
Software Engineering (10th Edition)
C++ How to Program (10th Edition)
Java How To Program (Early Objects)
Starting Out with C++: Early Objects (9th Edition)
- Write a method printSquares that uses recursive backtracking to find all ways to express an integer as a sum of squares of unique positive integers. For example, the call of printSquares(200); should produce the following output:1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 8^2 + 9^21^2 + 2^2 + 3^2 + 4^2 + 7^2 + 11^21^2 + 2^2 + 5^2 + 7^2 + 11^21^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^21^2 + 3^2 + 4^2 + 5^2 + 7^2 + 10^22^2 + 4^2 + 6^2 + 12^22^2 + 14^23^2 + 5^2 + 6^2 + 7^2 + 9^26^2 + 8^2 + 10^2Some numbers (such as 128 or 0) cannot be represented as a sum of squares, in which case your method should produce no output. Keep in mind that the sum has to be formed with unique integers. Otherwise you could always find a solution by adding 1^2 together until you got to whatever number you are working with.As with any backtracking problem, this one amounts to a set of choices, one for each integer whose square might or might not be part of your sum. In many of our backtracking problems we store the choices in…arrow_forwardWrite a method printSquares that uses recursive backtracking to find all ways to express an integer as a sum of squares of unique positive integers. For example, the call of printSquares(200); should produce the following output:1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 8^2 + 9^21^2 + 2^2 + 3^2 + 4^2 + 7^2 + 11^21^2 + 2^2 + 5^2 + 7^2 + 11^21^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^21^2 + 3^2 + 4^2 + 5^2 + 7^2 + 10^22^2 + 4^2 + 6^2 + 12^22^2 + 14^23^2 + 5^2 + 6^2 + 7^2 + 9^26^2 + 8^2 + 10^2Some numbers (such as 128 or 0) cannot be represented as a sum of squares, in which case your method should produce no output. Keep in mind that the sum has to be formed with unique integers. Otherwise you could always find a solution by adding 1^2 together until you got to whatever number you are working with.As with any backtracking problem, this one amounts to a set of choices, one for each integer whose square might or might not be part of your sum. In many of our backtracking problems we store the choices in…arrow_forward• Write a recursive method to complete the class BaseRaiseToN. • The method: public static int raiseBToN(int base, int exp) computes and returns the value of Bn using recursion.• What value/values of n will stop the recursion (base case)?• A precondition of this method is that B be a positive number greater than zero Sample Output Enter the base4Enter the exponent 3 4 to 3 is 64arrow_forward
- Write a recursive method public static int pos(int[] a, int l,int r) that positions a[l] at its rank if the array a were sorted between l and r,and that returns this rank. That is, we assume that a is an unsorted array of int,l and r are int such that 0 <= l <= r < a.length, after this call : k = pos(a, l, r); acontains the same elements before and after this call, but it is such that a[k] >=a[i] for all i in [l, k-1] and a[k] < a[j] for all j in [k+1, r].For instance, consider this fragment: int[] a = {8,7,4,1,9,6,2,5,3,0};System.out.println(Arrays.toString(a));// [8, 7, 4, 1, 9, 6, 2, 5, 3, 0]pos(a, 2, 7); // a[2..7] = {4, 1, 9, 6, 2, 5}System.out.println(Arrays.toString(a));/* [8, 7, 1, 2, 4, 6, 5, 9, 3, 0] is possible because4 >= 2, 4 >= 1, 4 < 6, 4 < 5, 4 < 9.But It could also be [8, 7, 2, 1, 4, 6, 5, 9, 3, 0], or[8, 7, 2, 1, 4, 9, 5, 6, 3, 0] */arrow_forwardWrite a recursive method called doubleDigits that accepts an integer n as a parameter and returns the integer obtained by replacing every digit of n with two of that digit. For example, doubleDigits(348) should return 334488. The call doubleDigits(0) should return 0. Calling doubleDigits on a negative number should return the negation of calling doubleDigits on the corresponding positive number; for example, doubleDigits(–789) should return –778899.arrow_forwardWrite a 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 Miaarrow_forward
- Write a 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 (until -1), and 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 -1 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 Julia partial code below, only lines 9-11, and 18 can be added onto, the rest must stay the same. #include <vector>#include <string>#include <iostream> using namespace std; // TODO: Write method to create and output all permutations of the list of names.void AllPermutations(const vector<string> &permList, const vector<string> &nameList) { } int main(int argc, char* argv[]) { vector<string> nameList; vector<string> permList; string name; // TODO: Read in a list of names; stop…arrow_forwardWrite a 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 (until -1), and use a recursive method to create and output all possible orderings of those names separated by a comma, one ordering per line. When the input is: Julia Lucas Mia -1 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_forwardBy Implement a recursive algorithm to find factorial of n,implement a recursive algorithm to find the n-th Fibonacci number.Print all the elements of a given array recursively. Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared). powerN(3, 1) → 3 powerN(3, 2) → 9 powerN(3, 3) → 27 please do not use any built-in function and complete this task using python 3.Do not copy-paste from other sources.arrow_forward
- write a recursive method that will sum up all elements in an array and returm the result. name the method as arraySumarrow_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_forwardWrite a recursive method that gets three parameters as input: an array of integers called nums, an integer called index,and an integer called The purpose of this method is to return true if value is stored in the array starting at nums[index]. That is, you have to check if value is equal to nums[index] or nums[index +1] or nums[index +2 ] …. nums[nums.length -1]. Do not use loops.(java code)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