Recursion Intro Assignment. You may not use for or
while. Any looping must be accomplished using recursion.
You may not declare static or non-static member fields – only local
or parameter variables allowed.
![Consider an array of integers. You wish to find an approximate median. You have an idea: split the array into three pieces, find the approximate median of each piece, and then calculate the median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find the approximate median of one element, that element itself is its own median. If we are working with two elements, take the mean (average) which may be a fractional value. Otherwise, we are working with at least three elements. There are three possibilities for the number of elements: it is divisible by 3, it has a remainder of 1 when divided by 3, or a remainder of 2 when divided by 3. If the number of elements divided by 3 has a remainder of O: each piece should be n/3 elements. If a remainder of 1: the first piece should consist of the first In/3] elements, the last piece should consist of the last In지 elements, and the middle piece is the remaining rn/31 elements. In/31 rounds up. For example, if there were 4 elements, the first piece is the first element, the last piece is the last element, and the middle two elements constitute the middle piece.) If a remainder of 2: the first piece should consist of the first [n/31 elements, the last piece should consist of the last [n/31 elements, and the middle piece is the remaining In/3] elements. For example, if there were 8 elements, the first piece is the first 3 elements, the last piece is the last 3, and the middle two elements constitute the middle piece.)](http://img.homeworklib.com/questions/947ba450-761e-11eb-b783-9bbc3b8f5795.png?x-oss-process=image/resize,w_560)

The code will be
public class MyClass {
public static double median3(int[] a)
{
if(a.length==1)return a[0];
else if(a.length==2)return (a[0]+a[1])/2.0;
else
{
return helper(a,0,a.length);
}
}
public static double helper(int a[],int start,int n)
{
double part1,part2,part3;
if(n==1)return a[start];
else if(n==2)return (a[start]+a[start+1])/2.0;
else
{
if(n%3==0)
{
part1=helper(a,start,n/3);
part2=helper(a,start+n/3,n/3);
part3=helper(a,start+(2*n)/3,n/3);
}
else if(n%3==1)
{
part1=helper(a,start,n/3);
part2=helper(a,start+n/3,n-n/3-n/3);
part3=helper(a,start+n-n/3,n/3);
}
else
{
part1=helper(a,start,n/3+1);
part2=helper(a,start+n/3+1,n/3);
part3=helper(a,start+n/3+n/3+1,n/3+1);
}
}
return medianOfThree(part1,part2,part3);
}
public static double medianOfThree(double a,double b,double
c)
{
return Math.max(Math.min(a,b), Math.min(Math.max(a,b),c));
}
public static void main(String args[])
{
int[] a={1};
System.out.println(median3(a));
int[] b={1,2};
System.out.println(median3(b));
int[] c={3,-10,100};
System.out.println(median3(c));
int[] d={1,2,-5,10,100,6};
System.out.println(median3(d));
int[] e={1,2,-10,7,20,-3,100,6};
System.out.println(median3(e));
int[] f={1,2,-20,-10,7,20,-3,100,6,92};
System.out.println(median3(f));
}
}
The output is

Do give a thumbs up
Recursion Intro Assignment. You may not use for or while. Any looping must be accomplished using...
Please Help me! Recursive approximate median Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find...
Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with alternating signs. // For example, sumTerms(7) returns the following: 1/1 – 1/2 + 1/3 – 1/4 + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign. ...
3. (24%) Write the following functions. You MUST use recursion for credit. You may use the string library functions: indexing, lengthO, +, and substr(int n) (recall that substr(n) returns a string with all but the first n characters). (a) string alternate(string s): returns a string that contains every other element of s starting at the beginning. For example, alternate( 'dog' ') ''dg'' (b) string uc(string s): returns a string that is the same as s except that every upper case...
Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to: Declare and instantiate arrays Access array elements by index Use loops and decisions to manipulate arrays and array elements Write methods that manipulate arrays Write methods that take array arguments Write methods...
Write a method called printReverse() that
takes a string and uses recursion to print the contents of the
string in reverse order. The string itself should
not be reversed; it must be left in its
original form.
The method has the following header:
void printReverse(String s, int i)
where s is a reference to the string, and i is an integer
parameter that you may use as you see fit. You do not need
to code up this method as...
Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops. Instructions This assignment will be different than previous assignments (and most assignments which come after it). In this assignment, you will be crafting four solutions to four different problems. This assignment will also have special requirements regarding how you may code. You are not allowed to use assignment statements. This includes using preincement, postincrement, predecrement, and postdecrement. You are allowed to use assignment to...
Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue. The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field). You should complete the software to implement and test your queue and submit the software along with a project report. Your queue class...
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...
NOTE – You may use C++ - but you MAY NOT use #include <string> The goal is to use C-Style character array based strings. You will put all of your code in this file. You may use any technique we have learned so far. Specifications: This assignment is split into several parts. The goal is to get you used to working with C/C++ run-time arrays and strings. Part 0 – Create a Menu You should create a menu that gives...
This Individual Assignment is a set of three problems. The first is a recursion "warm up" exercise, and the second two are QuickSort variations. The "warm up" should be implemented as a static method in your main App class and the second two will use additional classes (as instructed below). All three problems should be included in the same NetBeans project (exported to ZIP as usual). ----------------- All of your classes should be properly encapsulated, follow all proper coding conventions...