Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Outline, but do not implement in Java, a recursive solution for finding the s

ID: 655652 • Letter: A

Question

a. Outline, but do not implement in Java, a recursive solution for finding the smallest value in an array.

b. Show the Java code for a recursive method called reverse(String text) that returns a string with its characters in reverse order. For example, reverse("Hello!") returns the string "!olleH". One idea: You could implement a recursive solution by removing the first character, reversing the remaining text, and combining the two strings.

c. Show the java code for a method that uses recursion to compute the sum of all values in an array.

Explanation / Answer

a) a recursive solution for finding the smallest value in an array using c-

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10
#define MAX 100

int recursive(int a[],int low, int high);
int array[20];
int i;


int main(void) {

   int sum;
   srand(time(NULL));

   for (i = 0; i < SIZE; i++)
   {
       array[i]=rand()%100;
   }


   for (i = 0; i < SIZE; i++)
   {
       printf("%d ", array[i]);
   }

   sum=recursive(array, 0, SIZE);
   printf("Small value is %d.", sum);
   getch();
}


   static int small=MAX;

   if (b[low]<small && low<high)
   {
       small=b[low];
       recursiveMin(a, low+1, high);
   }
   if (b[low]>small && low<high )
   {
       recursiveM(a, low+1, high);
   }  

   return small;
}

B) Java code for a recursive method called reverse-

import java.io.FileNotFoundException;
import java.io.IOException;

public class StringReverse {

public static void main(String args[]) throws FileNotFoundException, IOException {

Strng str = "nisha is going to college";
System.out.println("Original String: " + str);

String reverStr = new StringBuffer(str).rever().toString();
System.out.println("Rever String using StringBuffer: " + reverStr);
reverStr = reverse(str);
System.out.println("Rever String using Iteration: " + reverStr);

reverStr = reverRecur(str);
System.out.println("Rever String using Recursion: " + reverStr);

}

public static String rever(String str) {
StringBuild strBuild = new StringBuild();
char[] strChars = str.toCharArray();

for (int i = strChars.length - 1; i >= 0; i--) {
strBuild.append(strChars[i]);
}

return strBuild.toString();
}

public static String reverseRecur(String str) {

if (str.length() < 2) {
return str;
}

return reverseRecur(str.substring(1)) + str.charAt(0);

}
}


C) java code for a method that uses recursion to compute the sum of all values in an array.-

public class RecursionSumValInArray {
public static int recurSum(int[] array, int ind) {
  
if (ind == array.length)
return 0;
  
else
return array[ind] + recurSum(array, ind + 1);
}
  
array.
public static int iterative(int [] array) {
  
int s = 0;
  
for (int i=0; i < array.length; i++)
s = s + array[i];
  
return s;
}
  
public static void main(String[] args) {
int[] array = {2, 5, 1, 3, 6, };
  
System.out.print( "Using recursion" );
System.out.println( recur(array, 0) );
  
System.out.print( "Using iteration" );
System.out.println( iterative(array) );
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote