Results 1 to 5 of 5

Thread: average

  1. #1
    anjigadu is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Smile 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 10:50 PM. Reason: Mod edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    anjigadu is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by anjigadu View Post
    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. #5
    anjigadu is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    ok thanks a lot for the reply .. I am new to this forum... I will try and surely post the if any help needed ...

Similar Threads

  1. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-13-2010, 12:59 AM
  2. Using threads to calculate the average
    By gish in forum Advanced Java
    Replies: 19
    Last Post: 06-19-2010, 03:57 PM
  3. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  4. Replies: 1
    Last Post: 03-11-2010, 05:21 AM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •