Results 1 to 5 of 5
Thread: average
- 09-19-2010, 09:39 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
average
Hi all I am new to this forum
How can I get the average of numbers in a two dimensional array help me please
Java Code:import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class numArray2D { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { int [][] nArray = new int[4][3]; float avg = 0; int sum =0; Scanner myScanner = new Scanner(new File("2DArrayData.txt")); for(int i=0;i<4;i++) { for(int j=0;j<3;j++) { nArray[i][j] = myScanner.nextInt(); System.out.print(" "+nArray[i][j]); } System.out.println("\n"); } for(int i=0;i<4;i++) { for(int j=0;j<3;j++) { sum = sum + nArray[i][j]; } } System.out.println(sum); for(int i=0;i<4;i++) { for(int j=0;j<3;j++) { avg = avg + nArray[i][j] ; <--------------------- here I am getting problem can any one correct and give a CORRECT CODE } } System.out.println(avg); } }
and thatQ :)Last edited by Fubarable; 09-19-2010 at 09:50 PM. Reason: Mod edit: code tags added
-
Your current avg variable holds a sum of all the numbers of the array. Ignoring Java for a moment and using just plain algebra, how do you calculate an average from a sum, if you know how many numbers were used? If you know this, you'll know how to fix your code.
Best of luck and welcome!
- 09-19-2010, 09:46 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
I know that I am using 12 integer numbers there ... but How can I fix 1 there I am unable to put that number (12) in the JAVA CODE FORM ... please explain me that
-
That's not how the forum works as it's your job to create the Java code, and then if it still doesn't work, we'll help you with it. So again, knowing that there are 12 numbers, how do you calculate the average from the sum? Then try to do this in your program, and if it still doesn't work, please post your new code.
Also, I've edited your post to add code tags so that your code retains its formatting and is readable. To do this yourself, simply edit your posts, highlight your code and press the "CODE" button.
Best of luck!
- 09-19-2010, 09:52 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09-12-2010, 11:59 PM -
Using threads to calculate the average
By gish in forum Advanced JavaReplies: 19Last Post: 06-19-2010, 02:57 PM -
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04-06-2010, 05:03 AM -
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03-11-2010, 04:21 AM -
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks