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

Write a program that computes statistical information above an array: mean (aver

ID: 3672777 • Letter: W

Question

Write a program that computes statistical information above an array: mean (average), mode (most frequent element) and median (the middle element, when the values are sorted from smallest to largest). Your program should define a method that takes an array of integers as a parameter and returns the appropriate value. There should be one method that computes the mean, one that computes the mode and one that computes the median. Note that you will need to implement a sort method in order to sort the list of numbers before you can compute the median. Your program should generate a list of random integers in the range from -100 to 100 (inclusive and then use that array with each of the method. The program should use three different sizes of the array: 100, 1,000, 10,000. For each of the (randomly generated) arrays it should print the three statistics. Do not print the actual array to the screen in the final program that you submit.

Explanation / Answer

//Change the size of the array in programm.

public class NumberStats {
static int[] arr=new int[100];
  
public static void main(String args[])
{
int Low = -100;
int High = 100;
Random r = new Random();
for(int i=0;i<100;i++)
{
arr[i]=(int)(r.nextInt(High-Low) + Low);
}
sortarray(arr);
System.out.println("Mean : "+mean(arr));
System.out.println("Median : "+median(arr));
System.out.println("Mode : "+mode(arr));
  
  
}
public static double mean(int[] m) {
double sum = 0;
for (int i = 0; i < m.length; i++) {
sum += m[i];
}
return sum / m.length;
}
public static double median(int[] m) {
int middle = m.length/2;
if (m.length%2 == 1) {
return m[middle];
} else {
return (m[middle-1] + m[middle]) / 2;
}
}
public static int mode(int a[]) {
int maxValue, maxCount=0;
  
maxValue=a[0];
for (int i = 0; i < a.length; ++i) {
int count = 0;
for (int j = 0; j < a.length; ++j) {
if (a[j] == a[i]) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
  
static void sortarray(int[] arr)
{
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
}

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