write a program that initializes an array with 30 integers then calculates the m
ID: 3631698 • 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 the above program using a different method:
**/ method that initializes the array
* @ returns an array of doubles
*/
Public static int[] initialize ()
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));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.