Results 1 to 6 of 6
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default 2 dimensional array, average student

    I'm building a 2d array:

    this is de the output

    Wk1,Wk2,Wk3,Wk4,Wk5,Wk6,Wk7
    Student 0| 1.0 9.0 7.0 6.0 5.0 7.0 6.0
    Student 1| 3.0 3.0 6.0 2.0 9.0 1.0 8.0
    Student 2| 6.0 1.0 7.0 9.0 9.0 3.0 8.0
    Student 3| 6.0 5.0 4.0 2.0 1.0 9.0 7.0
    Student 4| 2.0 6.0 9.0 9.0 6.0 5.0 2.0

    I can't seem to get the right average per row, what am i doing wrong:

    Java Code:
     public void fillResults(){
           double sum = 0.0;
           double average =0.0;
            
            for(row=0; row<students.size();row++)
            {
                for(column=0; column <results.length  ; column++)
                {
                   
                    results [row][column] = (int)(Math.random()*10);
                    sum += results[row][column];
    
                    if (results[row][column]==0.0)
                       results [row][column]=1.0;
                  
                }
            average = sum / 7;
            System.out.println("The average for student " + row + " is " + average);
    
            }
        }
    I think you can reset the sum to 0 every loop, but then its not punctual to.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    You're setting sum to 0.0 at the very beginning of the method, but you should set it to zero before you compute the sum of each row.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Staring to long

    Like this?
    I have to initialize sum, otherwise i get an error.

    But i get this output:
    The average for student 0 is 1.0
    The average for student 1 is 0.7142857142857143
    The average for student 2 is 0.42857142857142855
    The average for student 3 is 0.42857142857142855
    The average for student 4 is 0.2857142857142857

    Wk1,Wk2,Wk3,Wk4,Wk5,Wk6,Wk7
    Student 0| 1.0 2.0 4.0 1.0 7.0 9.0 5.0
    Student 1| 1.0 1.0 2.0 4.0 4.0 7.0 2.0
    Student 2| 6.0 1.0 1.0 9.0 7.0 8.0 7.0
    Student 3| 2.0 1.0 4.0 4.0 3.0 3.0 1.0
    Student 4| 1.0 2.0 9.0 8.0 2.0 5.0 9.0


    Java Code:
     public void fillResults(){
    
           double sum =0.0;
           double average =0.0;
            
            for(row=0; row<students.size();row++)
            {
                
                for(column=0; column <results.length  ; column++)
                {
                   sum=0.0;
                    results [row][column] = (int)(Math.random()*10);
                    sum += (double)results[row][column];
    
                    if (results[row][column]==0.0)
                       results [row][column]=1.0;
                  
                }
                average = sum / 7;
                System.out.println("The average for student " + row + " is " + average);
    
            }
        }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aborgeld View Post
    Like this?
    I have to initialize sum, otherwise i get an error.
    Yes you have to initialize sum but not at the location you did it; also add a cell value to sum after it is set to its final value; like this:

    Java Code:
     public void fillResults(){
    
           double sum;
           double average =0.0;
            
            for(row=0; row<students.size();row++)
            {
                sum= 0.0;            
                for(column=0; column <results.length  ; column++)
                {
                    results [row][column] = (int)(Math.random()*10);
    
                    if (results[row][column]==0.0)
                       results [row][column]=1.0;
                  
                    sum += (double)results[row][column];
                }
                average = sum / 7;
                System.out.println("The average for student " + row + " is " + average);
    
            }
        }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi Jos

    I have changed the things in my code into the changes in your example.
    It works fine now. Well its quite logical if you say it like this.

    Thanks for the example and most important for explaining. Because i'm still learning it!
    Kind regards,

    André

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aborgeld View Post
    Hi Jos

    I have changed the things in my code into the changes in your example.
    It works fine now. Well its quite logical if you say it like this.

    Thanks for the example and most important for explaining. Because i'm still learning it!
    Kind regards,

    André
    You're welcome of course and yep, that's how computers are: they do exactly what you tell them to do, not what you have in mind ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. 2 dimensional array
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 02-21-2011, 12:56 AM
  2. Can't get correct average using array
    By nevets93 in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 11:33 AM
  3. Two dimensional array
    By niu_niu in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 01:34 AM
  4. Delete Student from Array Help
    By kazit in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 03:42 AM
  5. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM

Posting Permissions

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