Thanks for taking the time to read my post. Like the title suggests, I need help on a program involving arrays.
The assignment requires that numbers from a text file be read into an array. From this array, the average the standard deviation of these numbers must be found. Then another array must be filled from the first array. From this second array up to two modes must be found.
The code is as follows:
Code:
class Runner
{
public static void main(String[] args)
{
Statistics stat = new Statistics();
}
}
import apcslib.*;
import chn.util.*;
public class Statistics
{
private String filename = "numbers.txt";
FileInput infile = new FileInput(filename);
private int number;
private int position;
private int[] Frequency;
private int numer;
private int total;
private int modeCount;
private int mode1;
private int mode2;
private double average;
private double devSum;
private double deviation;
private boolean noMode;
private int[] Data;
public Statistics()
{
noMode = false;
Data = new int[1000];
Frequency = new int[101];
MenuCon();
}
private void MenuCon()
{
System.out.println("Welcome to the Statistics Processer.");
readFile();
findavg();
findDev();
findmode();
printresults();
printmode();
}
private void readFile()
{
position = 0;
while(infile.hasMoreTokens())
{
number = infile.readInt();
Data[position] = number;
Frequency[Data[position]]++;
total++;
numer += number;
position++;
}
};
private void findavg()
{
average = (double)numer/total;
}
private void findDev()
{
for(position = 0; position < total; position++)
{
devSum += Math.pow(Data[position] - average,2);
}
deviation = devSum/(total - 1);
deviation = Math.sqrt(deviation);
}
private void findmode()
{
mode1 = 0;
for(position = 0; position < total; position++)
{
System.out.println(Data[position]);
if(Frequency[number] > mode1)
{
mode1 = Data[position];
modeCount++;
}
else if(Frequency[number] == mode1)
{
mode2 = Data[position];
modeCount++;
}
else if(modeCount > 2)
{
noMode = true;
}
Frequency[number]++;
}
}
private void printresults()
{
System.out.println("The average is: " + Format.left(average,2,2));
System.out.println("The standard deviation is: " + Format.left(deviation,2,2));
}
private void printmode()
{
if(modeCount == 1)
{
System.out.println("The mode is: "+ Format.right(mode1,2));
}
if(modeCount == 2)
{
System.out.println("The data is bimodal.");
System.out.println("\tThe first mode is: "+ Format.right(mode1, 2));
System.out.println("\tThe second mode is: "+ Format.right(mode2,2));
}
else if(noMode)
{
System.out.println("There is no mode.");
}
}
}
The trouble keeps on coming up in finding the mode. Any help would be apreciated. The file I am supposed to use is attached.