Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Location
    Denver, Colorado
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help needed on Arrays in Java

    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.
    Attached Files Attached Files

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You may want to explain more specifically what the problem is. Like what exactly happens that is different than what you expect. You also might want to explain what a "mode" is for those people who are not statistics wizards. I hated my stats class in college so I didn't pay close attention.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    May 2004
    Location
    Denver, Colorado
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the clarification Spookster.
    I'll explain a mode first. The mode of a set of numbers is the number that occurs most frequently. So if a series of numbers were to consist of the numbers [3 3 4 5], three would be the mode. However, if that same series was [3 3 4 4 5], then there would infact be two modes three and four. A series is called bimodal when this happens. But if there are three or more modes as in [3 3 4 4 5 5] there is no mode. This is because the frequency of the numbers starts to lose its significance when that frequency is so commonly occurring.
    Now I am running into problems with finding the mode of this series of numbers. The teacher of my class told me that the file is bimodal. My teacher wants two arrays. The first array stores each number from the file. The second array stores the how many times each number appears in the corresponding element in this array. So if the number three appears eight times, then in element three the number eight should be stored.
    Please tell me if that is not clear enough, and thanks again for taking the time to read this post.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •