1

This is all my code. I am having problems with the standard deviation formula. I run the program with these values:

Number of items: 5

Items: 16 25 81 80 24

I'm supposed to get this output:

Average:    45.20

Std Dev:    32.41

Less than Avg: 3

Array is not in sorted order

Instead, I get this output:

Array is not in sorted order

Average: 45.20

Std Dev: 55.60

Less than Avg: 3

import java.text.DecimalFormat;
import java.util.Scanner;
public class array {

public static void main(String[] args) {
Scanner input = new Scanner(System.in); 
DecimalFormat df = new DecimalFormat ("#.00");
System.out.println("How many values do you want?");
int num = input.nextInt(); 
if (num< 1 || num > 100)
{
    System.out.println("Error");
    System.exit(0);
}
int[] array= valueArray(input, num);
double o= average(num,  array);
double standdev = getStdDev(array, num); 
int lessThanAvg = lessAvg ( array, num, o );
boolean sorted=isArraySorted(array, num);
System.out.println("Average: " + df.format(o));
System.out.println("Std Dev: " + df.format(standdev));
System.out.println("Less than Avg: " + lessThanAvg);
}

public static int[] valueArray (Scanner input, int num )
{
    int[] values = new int[100]; 
    System.out.println("What numbers do you want to put in?");
    for (int j = 0; j < num; j++)
    {
        values[j]=input.nextInt();

    }
    return values;
}
public static double average ( int num ,int[] values)
{
    double avg=0.0;
    for (int i = 0; i < num; i++)
    {
        avg = avg+values[i];
    }

    return avg/num;
}

public static double getStdDev (int [] values, int num)
{
    double avg = 0.0;
    double sum = 0 ;
    for (int i = 0; i < num - 1; i++)
    {

        sum = Math.sqrt ((Math.pow((values[i]-avg),2) + Math.pow((values[num-1]),2)) / num-1);


    }
    return sum;

}
public static int lessAvg ( int [] values, int num, double avg )
{
    int counter = 0;
    for (int i = 0; i < num; i++ )
    {
        if (values[i] < avg)
        {
            counter = counter + 1;
        }
    }
    return counter;
}
public static boolean isArraySorted (int [] values, int num)
{
    for (int i = 0; i < num - 2; i++)
    {
        if (values[i]>values[i+1])
        {
            System.out.println("Array is not in sorted order");
            return false;

        }
    }

     System.out.println("Array is in sorted order"); 
    return true;
}
}
Sailendra
  • 1,318
  • 14
  • 29
snathan
  • 11
  • 3
  • You code is way more complex than it needs to be. Consider not using an array and moving around the number of actual elements; create an array of exactly that size that you will need, and later on just use the .length field of that array when iterating! Then: you want us to spend our time to help you. Sou you please spend the **one** minute it takes to really properly indent/format your source code. Finally: did you consider using a debugger or **printing** the steps of your computations? Why do you rely on other people to inspect your own code? – GhostCat Oct 24 '16 at 07:09
  • I must write this program in this format, I'm still a novice in java programming. I'm not very familiar with the debugger – snathan Oct 24 '16 at 07:13
  • In `stdDev` you are looping, but for every iteration you are replacing the value of `sum` – Scary Wombat Oct 24 '16 at 07:14
  • your getStdDev function is one big mess, why are you not passing average as a parameter to the function?. you're setting the average to 0 before starting your calculation. and your calculation is wrong even if you had set the average correctly – hassan arafat Oct 24 '16 at 07:15
  • I am talking about **indentation**, too. Just have a look how your main method above looks like! – GhostCat Oct 24 '16 at 07:23

1 Answers1

0

to get the standard deviation

  1. find out the mean.

  2. Then for each number of your array subtract the Mean and square the result.

  3. Then work out the mean of those squared differences

  4. find the square root of that.

For reference you can look at this Post

Community
  • 1
  • 1
Zia
  • 1,001
  • 1
  • 13
  • 25