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

2. 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!

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

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!

5. Member
Join Date
Sep 2010
Posts
3
Rep Power
0
ok thanks a lot for the reply .. I am new to this forum... I will try and surely post the if any help needed ...