This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dylan1999
Dylan1999 wrote ...

2016/4/7

Help with averaging a string/array of numbers?

Dylan1999 Dylan1999

2016/4/7

#
mport greenfoot.*; import java.awt.Color; /** * Write a description of class Array here. * * @author (your name) * @version (a version number or a date) */ public class Array extends Actor { private int numbers; private int temp; private int Index; private int total; private int average; /** * Array Constructor * * @param size A parameter */ public Array(int size) { numbers = new int; setImage(new GreenfootImage(400,500)); } /** * Method sortArray * */ public void sortArray() { for (int k = 0; k<numbers.length; k++) { for (int l = 0; l < 99; l++) if (numbers > numbers) { temp = numbers; numbers = numbers; numbers = temp; } } display(); } /** * Method reverseSort * */ public void reverseSort() { for (int n = 0; n<numbers.length; n++) { for (int m = 0; m < 99; m++) { if (numbers < numbers) { temp = numbers; numbers = numbers; numbers = temp; } } } display(); } /** * Method createArray * */ public void createArray() { for(int i = 0; i<numbers.length; i++) { numbers = Greenfoot.getRandomNumber(99)+1; } display(); } /** * Method calculate * */ public void calculate() { for(int a = 0; a < numbers.length - 1; a+=2) { total = numbers + numbers; average += total; image.drawString( + " ", x, y); } } /** * Method display * */ public void display() { GreenfootImage image = getImage(); image.clear(); image.setColor(Color.BLACK); image.drawString("The List of " + numbers.length+" Numbers", 40, 50); int y = 80; for (int i=0; i<10; i++) { int x = 40; y=y+35; for (int j=0; j<10; j++) { image.drawString(numbers + " ", x, y); Index++; x=x+35; } } if (Index == 100) { Index = 0; } } } So far I have code to sort/reverse sort my numbers. I'm really struggling with the average (under the method calculate).
danpost danpost

2016/4/7

#
For an average, you need two things -- the sum of the numbers and the count of the numbers. The sum divided by the count will be the average. So if you create a local variable for the sum and iterate through the numbers adding to the sum; then after the loop, divide the sum by 'numbers.length', you will have your average. What you have now in your calculate method is adding every other number in the array twice to the 'average' field (through the 'total' field). You also have the display call inside the loop where the average is a function of all the numbers together, not individually (you need to display the 'average' value after the loop (and after dividing the total by the count). Something else that is puzzling -- why are you using an Actor subclass for Array? Should not you bee using the world background or text panel actors to display an array that is internal (belonging to your World subclass object)? If you want a separate class for the Array, it should extend the Object class; but, there is no benefit in doing that over just having the array a field your world contains.
Dylan1999 Dylan1999

2016/4/7

#
My instructor gave a preset scenario that I was to use for coding. Can you explain what you mean by 'local variable for the sum'?
danpost danpost

2016/4/7

#
A local variable is one that is defined within a method or constructor. Defined variables through method parameters are also local. The 'local' word just means that the variable has no scope beyond the method. One the method is done executing, the variable is lost. For example, to add the numbers one through a given number:
private int sumTo(int limit) // 'limit' is a local variable
{
    int sum = 0; // 'sum' is a local variable
    for (int i=1; i<=limit; i++)
    {
        sum += i;
    }
    return sum;
}
You will be doing basically the same thing, except instead of returning the sum, you will be dividing by the 'limit' and drawing some text -- and you will not be adding the iterator to the sum, but using the iterator as an index in your array to get the value to add to the sum.
Dylan1999 Dylan1999

2016/4/8

#
I'm getting an incompatible type error.
danpost danpost

2016/4/8

#
Dylan1999 wrote...
I'm getting an incompatible type error.
On what line of code? Show the method that the highlighted line resides and indicate which line is highlighted.
Dylan1999 Dylan1999

2016/4/8

#
Okay, I'm no longer getting an incompatible type error nor a syntax error. However, I'm getting an Out of Bounds error with: public void calculate() { GreenfootImage image = getImage(); image.setColor(Color.BLACK); image.drawString("Average of " + sum, 40, 50); int sum = 0; for (int i=1; i<=numbers.length; i++) { sum += i; image.drawString(numbers + " ", 575 , 525); } }
danpost danpost

2016/4/8

#
Dylan1999 wrote...
Okay, I'm no longer getting an incompatible type error nor a syntax error. However, I'm getting an Out of Bounds error with: < Code Omitted >
Well your iterator is being used for indexing, not for the number itself (as I had used it for above). The 'for' line should be:
for (int i=0; i<numbers.length; i++)
and the 'drawstring' line should be outside the loop (after the next closing squiggly bracket) and should display something like "The average value of the "+numbers.length+" numbers is "+(sum/numbers.length)
You need to login to post a reply.