Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is my calstandardDev() returning 0?

 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it actually returns 0.0 , i've tried everything i can think of.
 
rahul V kumar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot that works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic