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

In this lab, you will learn the basics of array manipulation and recursion. Prob

ID: 3916980 • Letter: I

Question

In this lab, you will learn the basics of array manipulation and recursion. Problem: Write a program that given an array A of integers determines: e) f) Recursively the mode of the elements in A, the value that appears the most in A Iteratively the median of the elements in A. Note: each of the items above should be presented as a separate method. 1/ Describe how you want to solve this problem: write this description as a comment at the top of your java file. The description should be free of typos and grammatically correct. 2/ Put together a Java code that addresses the above problem. Each instruction of your code should be commented to explainwhat it does and why the whole piece of code is an actual solution to the above problem.

Explanation / Answer

/*

* *?

* In this program we defined two recursive methods for calculating Mode and Median of an integer array.

* for Mode we first define some integer variable to store value which can be used for getting mode.

After that we iterate the array with for loop and in this for loop we again iterate array with another

for loop to compare elements of array and save the element which come maximum time and save its count .

*/

public class IntegerArray {

static int mostAp;

public static void main(String[] args) {

int a[]= {11,21,32,11,44,21,11,6}; //Defining an integer array

int mode=mostAppeared(a); //Getting mode of array a[] using recursion

int median=median(a); //Getting median of array a[] using recursion

System.out.println("Mode of Array a[] is : "+mode); //Printing the value of mode

System.out.println("Median of Array a[] is : "+median); //Printing the value of median

}

public static int mostAppeared(int m[]) //recursive method for mode of array

{

int count=1;

int tempCount; //define the integer for temporary count the occurrence of most appeared value

int most=m[0]; //let the first element of array is mode of array

int temp=0; //define the integer to hold array values in iteration

for(int i=0;i<(m.length-1);i++)

{

temp=m[i];

tempCount=0;

for(int j =0;j<m.length;j++) //using for loop for iterating the array

{

if(temp==m[j]) //comparing the array values

tempCount++; //if value are same incrementing the temporary count

}

if(tempCount>count) //comparing the array value temporary count with count

{

most=temp; //if temporary count is grater then count than save temp element in most

count=tempCount; //temporary count in count

}

}

return most;

}

public static int median(int m[]) //recursive method for median of array

{

int sum=0;

for(int i=0;i<m.length;i++)

{

sum+=m[i]; //calculating the sum of all the array elements

}

return (sum/m.length); //return the median

}

}

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