aspose file tools*
The moose likes Beginning Java and the fly likes why is my calstandardDev() returning 0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why is my calstandardDev() returning 0?" Watch "why is my calstandardDev() returning 0?" New topic
Author

why is my calstandardDev() returning 0?

unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
import java.io.*;
public class Array
{
private int [] xIntegers;
private int numOfValues;
private double average;
private double sd;
public final int maxSize = 100;

public Array()
{
xIntegers = new int[maxSize];
numOfValues = 0;
average = 0000;
}
public void readValues()throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String inString = new String();
System.out.println("Enter the number of values in the array");
inString = br.readLine();
numOfValues = Integer.parseInt(inString);
xIntegers = new int [numOfValues];
for(int i = 0; i <numOfValues; i++)
{
System.out.println("Please enter the number in the array");
inString = br.readLine();
xIntegers[i] = Integer.parseInt(inString);
}

if (xIntegers.length > 1)
{
for (int i = 0; i < numOfValues; ++i)
average = average + xIntegers[i];
average = average/xIntegers.length;
}
System.out.println("The Average is" + " " + average);
System.out.println();
System.out.println("The Standard Deviation is" + " " + sd);
System.out.println();
}

public void writeValues()
{
for(int i= 0; i<numOfValues; i++)
{
System.out.println("[" + i +"] = " + xIntegers[i]);
}
}
public int getNumOfValues()
{
return numOfValues;
}
public double getAverage()
{
return average;
}
public double calStandardDev(int [] xIntegers, double average)throws IOException
{

if(xIntegers.length > 0)
{
for (int i = 0; i < xIntegers.length; ++i)
{
double sub = xIntegers[i] - average;
sd = sd + sub * sub;
}
sd = Math.sqrt(sd/xIntegers.length);
}
return sd;
}
}




import java.io.*;
public class TestArray
{
/**THE MAIN PROGRAM STARTS HERE, PROMPTS THE USER FOR
INPUT
AND ALLOWS THE USER TO SET THE SIZE OF THE ARRAY,
CALLS THE
APPROPRIATE METHODS TO DO THE COMPUTATIONS AND PRINTS
THE
RESULT **/
public static void main (String []args) throws
IOException
{
Array ta = new Array();
int numOfValues = ta.getNumOfValues();
int [] xIntegers = new int[numOfValues];
double average = ta.getAverage();

ta.readValues();
ta.writeValues();
ta.calStandardDev(xIntegers,average);
}
}
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Do you see anything wrong with doing division with integers? If you divide 1/2, what will the answer be if the answer must be an integer? Perhaps you should consider casting some stuff to floats or doubles and see if that makes things easier.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
it actually returns 0.0 , i've tried everything i can think of.
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
well there seems to be something wrong with the code

look at this statement
System.out.println("The Standard Deviation is" + " " + sd);

where is sd getting calculated.

shouldn't you call sd = calStandardDev(xIntegers,average);

try this, this should work for you.
System.out.println("The Standard Deviation is" + " " + calStandardDev(xIntegers,average));

hope this helps
[ October 22, 2004: Message edited by: rahul V kumar ]
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Thanks a lot that works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is my calstandardDev() returning 0?