zoukankan      html  css  js  c++  java
  • Java Program to Calculate Standard Deviation

    In this program, you'll learn to calculate the standard deviation using a function in Java.

    This program calculates the standard deviation of a individual series using arrays. Visit this page to learn about Standard Deviation.

    To calculate the standard deviation, calculateSD() function is created. The array containing 10 elements is passed to the function and this function calculates the standard deviation and returns it to the main() function.

    为了计算标准偏差,创建了calculateSD()函数。将包含10个元素的数组传递给该函数,此函数将计算标准偏差并将其返回给main()函数

    Example: Program to Calculate Standard Deviation

    public class StandardDeviation {
    
        public static void main(String[] args) {
            double[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            double SD = calculateSD(numArray);
    
            System.out.format("Standard Deviation = %.6f", SD);
        }
    
        public static double calculateSD(double numArray[])
        {
            double sum = 0.0, standardDeviation = 0.0;
            int length = numArray.length;
    
            for(double num : numArray) {
                sum += num;
            }
    
            double mean = sum/length;
    
            for(double num: numArray) {
                standardDeviation += Math.pow(num - mean, 2);
            }
    
            return Math.sqrt(standardDeviation/length);
        }
    }
    

    Note: This program calculates standard deviation of a sample. If you need to compute S.D. of a population, return Math.sqrt(standardDeviation/(length-1)) instead of Math.sqrt(standardDeviation/length) from the calculateSD() method.

    When you run the program, the output will be:

    Standard Deviation = 2.872281
    

    In the above program, we've used the help of Math.pow() and Math.sqrt() to calculate the power and square root respectively.

  • 相关阅读:
    3)小案例三,加乐前端入口index.php
    C语言中传值和C++的传引用
    2)小案例步骤2,添加工厂类
    1)小案例步骤一
    1)public,provite和protect不能放在函数函数头
    88)PHP,PDOStatement对象
    NET 中system.IO(Stream) 的学习笔记二
    c#中的char byte string 类型之间的转换
    字符集和字符编码(Charset & Encoding)
    c#window服务程序
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13022547.html
Copyright © 2011-2022 走看看