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

Write a program that includes two functions named and variance. The function sho

ID: 3848750 • Letter: W

Question

Write a program that includes two functions named and variance. The function should calculate and return the average of values stored in an array named testvals. The array should be declared in main and include the values 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82 and 73. The variance function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in, squaring the value obtained, adding them, and dividing by the number of elements in The values returned from calcavg() and variance() should be displayed by using cout statements in main.

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
  
public static double calaverage(int arr[])
{
double avg=0;
for (int i=0;i<arr.length;i++)
{
int value = arr[i];
avg=avg+value;
}
avg /= arr.length;
return avg; //it will return the average
  
}
public static double variance(int arr[])
{
double variance=0;
for(int i=0;i<arr.length;i++)
{
double diff= (calaverage(arr)- arr[i]); //substracting the average from each value
double square= Math.pow(diff,2); // squaring the result
variance +=square; // adding all the values
}
variance /= arr.length; //will give the varience
return variance;
}
  
   public static void main (String[] args) throws java.lang.Exception
   {
       int arr[]={89,95,72,83,99,54,86,75,92,73,79,75,82,73};
      
       System.out.println("The variance="+Codechef.variance(arr));
   }
}

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