0
votes

Using double float data calculate the count, average and standard deviation of any given input.

Every time I run the program it gives me my count and average however my standard deviation shows up as NaN.

import java.util.Scanner;

public class Final 
{
    public static void main (String[]args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a range of digits to recieive 1. Count , 2. Average , 3. StdDvn");

        double input = in.nextDouble();

        double count = 1;
        double sum = 0;
        double sumsquared = sum * sum;
        double std = 0;

        while (in.hasNextDouble())
        { double value = in.nextDouble();

            sum = input += value;
            count++;
        }

        double average = sum / count;
        std = Math.sqrt((sumsquared-(sum/count)) / (count - 1));

        System.out.println("Your count of numbers is : " + count);
        System.out.println("Your average of numbers is : " + average);
        System.out.println("Your standard deviation of numbers is : " + std);
    }
}
2
Everytime i run the program it gives me my count and average however my standard deviation shows up as NaN :/ - Ibbthebib
Use edit button to add more info, do not use comments for that. - Pavel Smirnov
Move sumsquared = sum * sum; down below the while loop. - RaminS
Thanks. Im inputting 1,2,3,4,5,6 and Im getting an output of 9.4 for standard deviation. it should be 1.8 i think. Where am i going wrong with stndrd deviation formula guys?? - Ibbthebib
The usual calculation of s.d. involves finding the average first, then calculating the sum of the squares of the differences of each item from the average, which requires iterating over the items a second time. It is difficult, but not impossible, to calculate the standard deviation without revisiting each input. Such an "online algorithm" exists to have only one pass, but for the average programmer it may be overkill to implement it. Can you just use a List? - rgettman

2 Answers

0
votes

Your sumsquared is zero. So, you’re using a value of 0 in your standard deviation. Here’s the fix to relocate your sumsquared.

EDIT: I think your std is incorrect. You must find the mean first. Inside the square root, find the sum of ( x - mean), where x are your data, then divide that result by count - 1.

public static void main (String[]args)
{


    Scanner in = new Scanner(System.in);

    System.out.print("Enter a range of digits to recieive 1. Count , 2. Average , 3. StdDvn");

    double input = in.nextDouble();

    double count = 1;
    double sum = 0;
    double std = 0;

    while (in.hasNextDouble())
    { double value = in.nextDouble();

        sum = input += value;
        count++;

    }

    double sumsquared = sum * sum;
    double average = sum / count;
    std = Math.sqrt((sumsquared-(sum/count)) / (count - 1)); /* fix this formula */

    System.out.println("Your count of numbers is : " + count);
    System.out.println("Your average of numbers is : " + average);
    System.out.println("Your standard deviation of numbers is : " + std);
    }
}
0
votes

Your sumsquaredvariable is always 0since you calculate it from 0*0 right after the initialization of double sum=0;.

This part should be moved below the summation.

Also to calculate the standard deviation without arrays using loops, you need to know the following 3 values:

  • How many numbers were entered.
  • The sum of all numbers.
  • The sum of the squares of the numbers.

Formula: E[X^2]-(E[X])^2 see wikipedia. ^2 means squared of course.

public static void main (String[]args)
{
    Scanner in = new Scanner(System.in);

    double count = 10.0;   // how many numbers are entered, e.g.: 10
    double sum1 = 0.0;    // sum of the numbers
    double sum2 = 0.0;    // sum of the squares
    System.out.println("Enter 10 numbers: ");
    for (int i=0; i < 10; i++) {
      double n = in.nextDouble();
      sum1 += n;
      sum2 += n * n;
    }
    double average = sum1 / count;
    double variance = (count * sum2 - sum1 * sum1) / (count * count);
    double stddev = Math.sqrt(variance);

    System.out.println("Your count of numbers is : " + count);
    System.out.println("Your average of numbers is : " + average);
    System.out.println("Your standard deviation of numbers is : " + stddev);
}