A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
standard deviation
unyime inok
Greenhorn
Joined: Oct 13, 2004
Posts: 29
posted
Nov 13, 2004 07:34:00
0
Please help me figure out why my calStandardDev() is giving an incorrect answer.
import java.io.*; public class TestRead { ReadInteger in = new ReadInteger(); final int numLength = 100; int num [] ; int x = 0; double average = 0.0; double sum = 0.0; public TestRead() { num = new int[numLength]; }
public double getAvg() { return average; } //read()- read integers from the file and calculates the average public void read()throws IOException{ while(!in.getEOF()) if (x < num.length){ x = x+1; num[x] = in.readInteger(); System.out.println(num[x]); //Calculates the average of the numbers in the file sum += num[x]; average = sum/(x-1); //end of average calculation } }
//Calculates the standard deviation of the numbers in the file public double calStandardDev(int [] num, double average) { double standardDev = 0.0; if(num.length>0){ for(x = 0; x < num.length; ++x){ double sd = num[x] - average; standardDev += sd*sd; } standardDev = Math.sqrt(standardDev/(num.length));//end of sDev } return standardDev; }
public void printVal() { System.out.println("The Average is:" + " " + average); System.out.println("The Standard is:" + " " + calStandardDev(num,average)); } //main method public static void main(String []args)throws IOException{ TestRead tr = new TestRead(); tr.read(); tr.printVal(); } }//end of TestRead class
unyime inok
Greenhorn
Joined: Oct 13, 2004
Posts: 29
posted
Nov 13, 2004 22:36:00
0
Please help me figure out why my calStandardDev() is giving an incorrect answer
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
posted
Nov 14, 2004 00:22:00
0
Hi Unyime,
Please refer to this
similar thread
posted by you.
Joyce
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: standard deviation
Similar Threads
ArrayIndexOutOfBounds help!
reading integer from a file
simple array loop
reading an integer from a file
please help figure out why the standard deviaion is not giving the correct output
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter