Results 1 to 5 of 5
  1. #1
    shadow240 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default array averages please help

    How can I find the overall averages of the columns in this array?
    when i run my code it gives me an output that looks like this (showing me wrong averages):

    0 1 2 3 overall grade
    Badger,Bradley 37 70 51 48 68.66666666666667
    Emu,Emma 35 81 75 40 72.83333333333334
    Aardvark,Alice 42 85 22 0 65.11111111111111
    Dodo,Donald exc 12 25 0 54.17391304347826
    Cassowary,Cassie 50 97 72 68 62.758620689655174

    my code is :

    Java Code:
     public static double[] computeAllGrades(int[][] scoreTable, int[] itemPointValues)
            {
                   
                     {
                 double totalPoints = 0;
                 double totalPointsPossible = 0;
                 double allGrades[];
                 double grade =0;
               
                 allGrades= new double[scoreTable.length];
                 int k = 0;
               
                 for(int j = 0 ; j < scoreTable.length ; j++)
                 {
                         for(int i = 0 ; i < scoreTable[0].length ; i++)
                         {
                                 if(scoreTable[j][i] == -1)
                         {
                                 totalPoints = totalPoints + 0;
                                 totalPointsPossible = totalPointsPossible + 0;
                               
                         }
                         else
                         {
                                 totalPoints = totalPoints + scoreTable[j][i];
                         totalPointsPossible = totalPointsPossible + itemPointValues[i];
                         }
                                         grade = (totalPoints/totalPointsPossible)*100;
                                       
                         }
                         
                         allGrades[k] = grade;
                         k++;
                 }
               
               
                 return allGrades;}}
    Last edited by shadow240; 11-06-2014 at 12:26 AM. Reason: formatting

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: array averages please help

    First, please place your code between code tags [code][/code] to preserve formatting. You need to sprinkle some print
    statements in your code to make certain you are averaging the correct values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shadow240 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: array averages please help

    i am not sure what to do next

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: array averages please help

    Try debugging the code to see what it is doing that is unexpected.

    Add some println() statements as Jim suggested to show the value of variables as their value is changed. The print out will show you what the computer sees when it executes the code.
    Last edited by Norm; 11-06-2014 at 02:40 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: array averages please help

    Line #28 inside that doubly nested loop is very suspicious ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 7
    Last Post: 01-20-2012, 09:44 PM
  2. Looping over averages in an int[][] array
    By adventurerOK in forum Advanced Java
    Replies: 1
    Last Post: 09-28-2011, 09:16 PM
  3. Beginner Loops for averages
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 10-17-2010, 09:17 PM
  4. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:51 PM
  5. Replies: 4
    Last Post: 04-16-2008, 01:26 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
  •