0

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);
    }
}
AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
Ibbthebib
  • 1
  • 1
  • Everytime i run the program it gives me my count and average however my standard deviation shows up as NaN :/ – Ibbthebib Jul 18 '19 at 19:00
  • 2
    Use [edit](https://stackoverflow.com/posts/57101064/edit) button to add more info, do not use comments for that. – Pavel Smirnov Jul 18 '19 at 19:04
  • Move `sumsquared = sum * sum;` down below the while loop. – RaminS Jul 18 '19 at 19:05
  • 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 Jul 18 '19 at 19:08
  • 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](http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf), but for the average programmer it may be overkill to implement it. Can you just use a `List`? – rgettman Jul 18 '19 at 19:35
  • Try these sites. https://www.programiz.com/java-programming/examples/standard-deviation or https://stackoverflow.com/questions/1735870/simple-statistics-java-packages-for-calculating-mean-standard-deviation-etc or https://stackoverflow.com/questions/18390548/how-to-calculate-standard-deviation-using-java note that these have been closed as off topic. – htm11h Jul 18 '19 at 20:16
  • @Ibbthebib: I added the whole `main()` for you. Take a look ... @htm11h: How can calculating a std.deviation be `off topic`? A duplicate I would understand, but off topic? – avermaet Jul 18 '19 at 20:26
  • @avermart never said I agreed, I was really just point it out to make you aware. I am firm believer there's no such thing as a bad question, but I understand frustrations when there is a lack of research. – htm11h Jul 19 '19 at 19:59

2 Answers2

1

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);
}
avermaet
  • 1,543
  • 12
  • 33
0

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);
    }
}
ManLaw
  • 115
  • 1
  • 10
  • Ive tried this but still standard deviation comes up as 9.4 wrong figure – Ibbthebib Jul 18 '19 at 19:40
  • I updated my answer. Your std formula is incorrect. – ManLaw Jul 18 '19 at 19:41
  • Here’s the standard deviation formula https://en.m.wikipedia.org/wiki/Standard_deviation – ManLaw Jul 18 '19 at 19:48
  • Might take another route and try the for statement – Ibbthebib Jul 18 '19 at 19:52
  • You’ll need to remove the first read from the scanner `input`. You don’t need to read at that point in the program. Change the sum formula inside the while loop to `sum += value`. Fix the standard deviation formula, and I think you’ll have it all done. – ManLaw Jul 18 '19 at 19:58