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

The file below is the starter file for the question: public class ArrayStats { p

ID: 3813827 • Letter: T

Question

The file below is the starter file for the question:

public class ArrayStats {
public static void main(String[] args) {
int[] array = new int[10];
  
for (int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random() * 100);
System.out.printf("array[%d] = %d ", i, array[i]);
}
System.out.println("");
  
double[] stats = getArrayStats(array);
  
System.out.printf("The average is %.2f ", stats[0]);
System.out.printf("The number of elements above average is %d ", (int)stats[1]);
int indexOfMax = (int)stats[2];
System.out.printf("The largest element is %d and its index is %d ", (int)array[indexOfMax], indexOfMax);
int indexOfMax2 = (int)stats[3];
System.out.printf("The second largest element is %d and its index is %d ", (int)array[indexOfMax2], indexOfMax2);
}
  
public static double[] getArrayStats(int[] array) {
// Your code here
  

In the file, provide code for the method

public static double[] getArrayStats(int[] array)

that takes a given integer array and returns the following statistics in an array of four doubles:

The average of the elements of the array.

The number of elements above average.

The indices of the largest and the second largest elements of the array.

In the returned array, have the average appear at index 0, the number of elements above average at index 1, and the indices of the largest and the second largest elements at index 2 and index 3 respectively.

Run the program to test the correctness of your code for the method. Below is a sample run:

array[0] = 40

array[1] = 97

array[2] = 93

array[3] = 64

array[4] = 19

array[5] = 68

array[6] = 74

array[7] = 61

array[8] = 30

array[9] = 72

The average is 61.80

The number of elements above average is 6

The largest element is 97 and its index is 1

The second largest element is 93 and its index is 2

Please also provide comments for each line of the program.

must also be written in java C++

Explanation / Answer

ArrayStats.java

public class ArrayStats {
public static void main(String[] args) {
int[] array = new int[10];
  
for (int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random() * 100);
System.out.printf("array[%d] = %d ", i, array[i]);
}
System.out.println("");
  
double[] stats = getArrayStats(array);
  
System.out.printf("The average is %.2f ", stats[0]);
System.out.printf("The number of elements above average is %d ", (int)stats[1]);
int indexOfMax = (int)stats[2];
System.out.printf("The largest element is %d and its index is %d ", (int)array[indexOfMax], indexOfMax);
int indexOfMax2 = (int)stats[3];
System.out.printf("The second largest element is %d and its index is %d ", (int)array[indexOfMax2], indexOfMax2);
}
  
public static double[] getArrayStats(int[] array) {
// Your code here
   double stats[] = new double[4];
   int sum = 0;
   for(int i=0; i<array.length; i++) {
       sum = sum + array[i];
   }
   double average = sum/(double)array.length;
   stats[0] = average;
   int numberOfElementsAbove = 0;
   int firstMax = array[0];
   int secondMax = array[1];
   int firstMaxIndex = 0;
   int secondMaxIndex = 1;
  
   for(int i=0; i<array.length; i++) {
       if(array[i] > average){
           numberOfElementsAbove++;
       }
           if (array[i] > firstMax) {
               secondMax = firstMax;
               firstMax = array[i];
               firstMaxIndex = i;

           } else if (array[i] > secondMax) {
               secondMax = array[i];
               secondMaxIndex = i;
           }
      
   }
  
   stats[1] = numberOfElementsAbove;
   stats[2] = firstMaxIndex;
   stats[3]=secondMaxIndex;
   return stats;
}

}

Output:

array[0] = 84
array[1] = 44
array[2] = 17
array[3] = 43
array[4] = 65
array[5] = 65
array[6] = 97
array[7] = 28
array[8] = 32
array[9] = 75

The average is 55.00
The number of elements above average is 5
The largest element is 97 and its index is 6
The second largest element is 84 and its index is 0

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