SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:5 | ISBN:9780132830317 | Edition: 5

Question

The standard deviation of a list of numbers is a measure of how much the numbers deviate from the average. If the standard deviation is small, the numbers are clustered close to the average. If the standard deviation is large, the numbers are scattered far from the average. The standard deviation of a list of numbers , , , and so forth is defined as the square root of the average of the following numbers:

, , , and so forth.

The number a is the average of the numbers , , , and so forth.

Define a static method that takes a partially filled array of numbers as its argument and returns the standard deviation of the numbers in the partially filled array. Because a partially filled array requires two arguments, the method should actually have two formal parameters, an array parameter and a formal parameter of type int that gives the number of array positions used. The numbers in the array should be of type double. Write a suitable test program

for your method.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

import java.util.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Main 
{
    public static void main(String args[])
	{
	    int sizeArray = 6;
		double[] nums = new double[10];
		DecimalFormat df=new DecimalFormat("#.##");
		nums[0] = 12.5;
		nums[1] = 53.0;
		nums[2] = 75.3;
		nums[3] = 89.12;
		nums[4] = 65.5;
		nums[5] = 80.0;
		System.out.println("The average standard deviation of a list of numbers is "
                            +df.format(standardDevList(nums, sizeArray)));
	}
	public static double standardDevList(double[] n1, int n2)
	{
		double avg = 0;
	    double avgStdDev = 0;
        for(int index = 0; index < n2; index++)
		{
			avg += n1[index];
		}
		avg /= n2;
	
		for(int index2 = 0; index2 < n2; index2++)
		{
			avgStdDev += (n1[index2] - avg) *
					  (n1[index2] - avg);
		}
		avgStdDev = Math.sqrt(avgStdDev / n2);
		return avgStdDev;
	}

}

Output of the program:

The average standard deviation of a list of numbers is 25.09

 

0 0

Discussions

Post the discussion to improve the above solution.