# Calculating Average using Arrays....help!

• 12-05-2011, 10:47 PM
knightwriter
Calculating Average using Arrays....help!
I've been stuck on this for many hours now. I'm trying to allow the user to input 10 numbers. Then the program is to add up the numbers and output an average. I'm baffled by what I should be doing for the average part. Below is my code, followed by my output. I actually only have to write out the logic for this code in pseudocode, but I find it easier to grasp what is needed by actually coding it in java first.

package handson7;

/**
*
* @author Matt
*/
import java.util.Arrays;
import java.util.Scanner;
public class HandsOn7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int index = 0;
int number;

int[] anArray;
int element;

anArray = new int[10];

number = 0;
element = 1;

while(number < 10){
System.out.println("Enter a number for " + element);
Scanner elementNumber = new Scanner(System.in);
anArray[index] = elementNumber.nextInt();
index = index + 1;
element = element + 1;
number = number + 1;
}
System.out.println("Here are the numbers you entered: ");
System.out.println(Arrays.toString(anArray));

int sum = 0;
for(int r = 0; r <anArray.length ; r++){
sum = sum + anArray[r];
int average = sum / anArray[r];
System.out.println("Here is the average " + average);
}

}

}

****Now the output that I have from this code*********

run:
Enter a number for 1
1
Enter a number for 2
1
Enter a number for 3
1
Enter a number for 4
1
Enter a number for 5
1
Enter a number for 6
1
Enter a number for 7
1
Enter a number for 8
1
Enter a number for 9
1
Enter a number for 10
1
Here are the numbers you entered:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Here is the average 1
Here is the average 2
Here is the average 3
Here is the average 4
Here is the average 5
Here is the average 6
Here is the average 7
Here is the average 8
Here is the average 9
Here is the average 10
BUILD SUCCESSFUL (total time: 11 seconds)
• 12-06-2011, 04:29 AM
Fubarable
Re: Calculating Average using Arrays....help!
That's not how you calculate averages -- the denominator is the number of items. And you'll want to do your calculation after you've calculated the sum, not while you're calculating it.
• 12-06-2011, 06:55 AM
Herah
Re: Calculating Average using Arrays....help!
int numElements = 0;
sum = 0;
//while loop code to enter numbers{
sum += number
numElements++
}

double average = sum / numElements
println(average)
• 12-06-2011, 02:15 PM
knightwriter
Re: Calculating Average using Arrays....help!
Thanks for all the help. This did the trick.
• 12-06-2011, 05:46 PM
Dark
Re: Calculating Average using Arrays....help!
Quote:

Originally Posted by Herah
int numElements = 0;
sum = 0;
//while loop code to enter numbers{
sum += number
numElements++
}

double average = sum / numElements
println(average)

Wow, now I can guarantee you that the OP has no idea what your code does and why his doesn't work. Thank you for spoonfeeding someone so now they will have difficulty throughout their entire Java endeavor.
• 12-06-2011, 10:02 PM
knightwriter
Re: Calculating Average using Arrays....help!
Actually, I do understand this and was able to come up with someone similar in a for loop.