4 Replies - 497 Views - Last Post: 16 August 2012 - 04:05 PM Rate Topic: -----

#1 willphill02  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 24-July 12

Can't calculate the median of an Array?

Posted 14 August 2012 - 05:01 PM

Hi, im trying to produce a program that calculates the mean,total and median of an array of 20 numbers entered in via a textfield. The total and mean calculations seem to work fine, but when i put in the calculation for the median everything goes wrong. I'm sure the code is right for calculating the median, but when its entered although it compiles it messes up the the calculations for the total and the mean and produces an incorrect answer itself. Here is the code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class whileloopq extends Applet implements ActionListener
{
	Label label;
	TextField input;
	int num;
	int index;
	int[] numArray = new int[20];
	int sum;
	int total;
	double avg;
	int median;



	public void init ()
	{
		label = new Label("Enter numbers");
		input = new TextField(5);
		add(label);
		add(input);
		input.addActionListener(this);
		index = 0;
	}

	public void actionPerformed (ActionEvent ev)
	{
        int num = Integer.parseInt(input.getText());
		numArray[index] = num;
		index++;
		if (index == 20)
		input.setEnabled(false);
			input.setText("");
	    sum = 0;
		for (int i = 0; i < numArray.length; i++)
		{
			sum += numArray[i];
		}
        total = sum;
        avg = total / index;

        Arrays.sort(numArray);
		int middle = ((numArray.length) / 2);
		if(numArray.length % 2 == 0){
		int medianA = numArray[middle];
                int medianB = numArray[middle+1];
	        median = (medianA + medianB) / 2;
		} else{
		 median = numArray[middle + 1];
		}

        repaint();

	}



	public void paint (Graphics graf)
	{



		graf.drawString("Total   = " + Integer.toString(total), 25, 85);
		graf.drawString("Average = " + Double.toString(avg), 25, 100);
		graf.drawString("Median = " + Integer.toString(median), 25, 115);



	}
}



My thoughts are that because the array is set to a limit of 20 this causes the numArray.length variable to always be a value of 20 however during input the actual length of the array is changing. Is there a way to not set a limit on the array? Any help would be much appreciated?

Will

Is This A Good Question/Topic? 0
  • +

Replies To: Can't calculate the median of an Array?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2001
  • View blog
  • Posts: 4,868
  • Joined: 10-September 10

Re: Can't calculate the median of an Array?

Posted 15 August 2012 - 12:44 AM

I added the following statement after the repaint(); statement at line 57:

System.out.println( Arrays.toString( numArray ) );

It may help you see what's going on.
Was This Post Helpful? 1
  • +
  • -

#3 willphill02  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 24-July 12

Re: Can't calculate the median of an Array?

Posted 15 August 2012 - 05:14 PM

Thanks for your reply, i can see that for some reason all the odd numbers are eventually removed from the array, this i do not understand. Also is there a way of not setting a limit to the array so that it just increases in size when a new number is added? Sorry I know this probably seems really simple but i'm a beginner to java.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2001
  • View blog
  • Posts: 4,868
  • Joined: 10-September 10

Re: Can't calculate the median of an Array?

Posted 15 August 2012 - 06:44 PM

I disagree with your conclusions from the behavior shown by the println statement. You should think about that some more.

Here's a little food for thought: numArray is initially filled with zeros. When a number > 0 is added to the array at index 0 and the array is sorted, at what index does the number added end up? (The first println result gives you the answer.) Then as you add more numbers to index 1, 2, 3, etc., at what indices do those numbers appear after being sorted? (Subsequent println results give you the answer.) Finally, when you add the next number to index = 10, what happens?

If you can't use an ArrayList to solve the problem just discovered above, then you can build something that acts like an array list with an array making tiny changes to your current code. One of those changes that may not be obvious is to sort a specific range of the array after a new number is added rather than the whole array. That statement looks like:

Arrays.sort( numArray, startIndex, endIndex );

Pursuing that should answer your question about limiting the array. You're really very close to finishing this.
Was This Post Helpful? 1
  • +
  • -

#5 willphill02  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 24-July 12

Re: Can't calculate the median of an Array?

Posted 16 August 2012 - 04:05 PM

Thank you, I've managed to figure it out now. I added the line:

Arrays.sort(numArray, 0, index);

and it works now. What you said really helped, I was looking past what was really going on. I think it was also possible with an if statement e.g.

if (index == 20)
{
Arrays.sort(numArray);
}

However this only sorted the array after all 20 numbers were entered

Thanks again for your help.

Will
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1