Question

In: Computer Science

JAVA: By comparing and contrasting recursive vs iterative algorithms.“How does the Sum_Recursive() function work and How...

JAVA:

By comparing and contrasting recursive vs iterative algorithms.“How does the Sum_Recursive() function work and How does each recursive call reduce the problem?

The Code:

public static void main(String[] args) {

int value, result;
value = 9;
result = Factorial_Iterative(value);
System.out.println("Factorial of " + value + " using (Iterative Calculation) is: " + result);
result = Factorial_Recursive(value);
System.out.println("Factorial of " + value + " using (Recursive Calculation) is: " + result);

int theArray[] = {9, 3, 2, 4};

result = Sum_Iterative(theArray, theArray.length);
System.out.println("Sum of array elements { 9, 3, 2, 4 }:" + "using (Iterative Calculation) is: " + result);

result = Sum_Recursive(theArray, theArray.length);
System.out.println("Sum of array elements { 9, 3, 2, 4 }:" + "using (Recursive Calculation) is: " + result);

}

public static int Factorial_Iterative(int n) {

int result = 1;
for (int i = n; i >= 1; i--) {
result = result * i;
}
return (result);
}

public static int Factorial_Recursive(int n) {

System.out.println(n);

if (n == 0) // base case
{
return 1;
} else {
return n * Factorial_Recursive(n - 1);
}
}
//--------------------------------------------------------------

public static int Sum_Iterative(int array[], int ArraySize) {
int result = 0;

for (int i = 0; i < ArraySize; i++) {
result = result + array[i];
}
return (result);

}
//--------------------------------------------------------------

public static int Sum_Recursive(int array[], int ArraySize) {
if (ArraySize == 1) {
return (array[0]);
} else {
return (array[ArraySize - 1] + Sum_Recursive(array, ArraySize - 1));
}
}

}

Solutions

Expert Solution

First of all what is recursive function:

A function which is being called by iteslf, or a calling to same function inside the same function body.

So, Sum_Recursive function is a recursive function because it calls to itself at line:

return (array[ArraySize - 1] + Sum_Recursive(array, ArraySize - 1));

so, what does this line means and how its working to calculate sum=18.

Explanation:

At first call when Sum_Recursive called by main function:

array[]: {9, 3, 2, 4} arraySize: 4

if,

inside the function we check is arraySize is equals to 1, if it is then retun array[0]=9 but at first call arraySize is 4 so this will not execute the if condition.

else,

it returns array[arraySize-1] which is =4 plus Sum_Recursive(array, ArraySize-1), so here calling again to sum_recursive without returning the result yet, so result will be (4 + Sum_Recursive(array, ArraySize - 1)).

then calling will again occur the if condition will not execute because array size 3 so in elese part, the previouse result 4 and this time return array[arraySize-1] which is =3 so result will be (4+3+Sum_Recursive(array, ArraySize - 1)).

again calling if condition will not execute because arraySize = 2

so else part will run and result will be (4+3+2 +Sum_Recursive(array, ArraySize - 1)) and dont forget sum_recursive is calling again to same function.

now arraySize will be 1 if condtion will true and it returns array[0] which is 9

so, result will result = (4+3+2 +9) = 18

which is the last recursive call and whole sum 18 will be returned to main function.

//OUTPUT//

Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the answer :)


Related Solutions

design two algorithms, an iterative version and a recursive version , for finding the min and...
design two algorithms, an iterative version and a recursive version , for finding the min and max values in an array of n numbers A.) a iterative version B.) a recursive version derive the time efficiency functions (or time function in terms of n) for each, analyze each function, and compare and discuss the results of the analysis
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the...
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the digits and display the sum. Example: ​​The sum of the number 5432 would be 14. o Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from the tail to the beginning. o...
There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT