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

write a program that initializes an array with 30 integers then calculates the m

ID: 3631697 • Letter: W

Question

write a program that initializes an array with 30 integers then calculates the mean value and standard deviation (is calculated as sqrt(((value i - mean)^2)/n). the program then print the values on three separate lines , 10 integers per line (separated by few blanks), and prints the mean and the standard deviation. for testing please use the values 1.....30.
!!!!!!!!!!Question to be answered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
redo above problem using different methods for each task:

/** method that initialize the array
* @ param data: array created in the main, to be filled in the method
*/
Public static void initialize (int[] data)

**/ method to calculate the main value
* @ param data: array whose contents are to be averaged
* @ return the main value
Public static double mean (int[] data)

/** method to calculate the standard deviation
* @ param data: array of values for which the standard deviation should be calculated
* @ return the standard deviation
Public static double standardDeviation (int[] data)

**/ main coordinates creates the array and calls the other methods.
* outputs the value of the mean and standard deviation
Public static void main (String[] args)

Explanation / Answer

public static void initialize(int[] data)
{
for(int i = 0; i < data.length; i++)
data[i] = i+1;
}

public static double mean(int[] data)
{
int sum = 0;

for(int i : data)
sum += i;
return (double)sum/data.length;
}

public static double standardDeviation(int[] data)
{
double variance = 0.0;
double mean = mean(data);

for(int i : data)
variance += (i-mean)*(i-mean);
variance = variance/data.length;
return Math.sqrt(variance);
}

public static void main(String[] args)
{
int[] data = new int[30];
initialize(data);
// print data
for(int i = 0; i < 30; i++)
{
if(i%10 == 0)
System.out.println();
System.out.print(data[i]+" ");
}
// output mean and standard deviation
System.out.println();
System.out.println("Mean: "+mean(data));
System.out.println("Standard deviation: "+standardDeviation(data));
}