Results 1 to 6 of 6
 12052011, 09:47 PM #1Member
 Join Date
 Nov 2011
 Posts
 49
 Rep Power
 0
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)

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.
 12062011, 05:55 AM #3Member
 Join Date
 Nov 2011
 Posts
 10
 Rep Power
 0
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)
 12062011, 01:15 PM #4Member
 Join Date
 Nov 2011
 Posts
 49
 Rep Power
 0
Re: Calculating Average using Arrays....help!
Thanks for all the help. This did the trick.
 12062011, 04:46 PM #5
Re: Calculating Average using Arrays....help!
 Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
 +Rep people for helpful posts.
 12062011, 09:02 PM #6Member
 Join Date
 Nov 2011
 Posts
 49
 Rep Power
 0
Similar Threads

Inputting arrays to find the average
By DuncanHollows in forum New To JavaReplies: 19Last Post: 07282011, 06:55 PM 
Displaying and calculating 2D arrays
By JSTBO in forum Java 2DReplies: 5Last Post: 03072011, 01:07 AM 
finding average in 2d arrays
By the beginner in forum New To JavaReplies: 18Last Post: 02242011, 04:32 AM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 05:03 AM 
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03112010, 04:21 AM
Bookmarks