1 2 Previous Next 21 Replies Latest reply: Feb 9, 2009 5:34 PM by 699554 Go to original post RSS
      • 15. Re: Traversing a 2d array...
        699554
        So when I have a 2d array of 4x4 it works perfectly fine. When they are not squares it seems to go out of
        bounds...
        Gah i was thrown off track with this comment thinking that the 2d array could be jagged. So if this is the case, then your solution should be straight forward. You could even determine the height and width of the 2d array before processing any elements.

        Mel
        • 16. Re: Traversing a 2d array...
          807588
          So...this doesn't make any sense...
          int numOfCols = mat.length;
          int numOfRows = mat[0].length;
          
          //For rows
          for(int i = 0; i < numOfCols; i++)
          {
               sumOfRow = 0;
               for(int j = 0; j < numOfRows; j++)
               {
                    sumOfRow = sumOfRow + mat[i][j];
               }
          }
          //For cols
          for(int i = 0; i < numOfRows; i++)
          {
               sumOfCol = 0;
               for(int j = 0; j < numOfCols; j++)
               {
                    sumOfCol = sumOfCol + mat[j];
               }
          }
          When comparing to this...the only thing that is technically different is instead of array...I used array[0]....and it works just fine now. I am happy I finished it, but it still makes no sense to me why one worked and the other did not.

          I hate coding sometimes.
          /reliefandanger
          • 17. Re: Traversing a 2d array...
            699554
            Ill point out some of your previous errors for you, hope i can explain this well.

            [0,0] [0,1]
            [1,0] [1,1]
            [2,0] [2,1]
            //Comments on first iteration of the 2d array above
            for(int i = 0; i < mat.length; i++)   //mat.length = i = 3
            {
                 sumOfCol = 0;
                 for(int j = 0; j < mat.length; j++) //mat[0].length = j = 2
                 {
            //i = 2 < max.length; j = 1 < mat[0].length
                      sumOfCol = sumOfCol + mat[j][i]; //mat[1][2] is out of bounds <<== UNDERSTAND?
                 }
            }
            Mel                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 18. Re: Traversing a 2d array...
              JoachimSauer
              mindloss wrote:
              When comparing to this...the only thing that is technically different is instead of array...I used array[0]....
              That's not the only difference.

              In this example i goes through different values in the first example and the second. The same is true for j.

              This is actually a nice example why you should give your variables names that tell you what they contain. I myself often fall in the trap of calling loop variables i and j. But if you called them currentRow and currentColumn in this example, then you would have quickly found the problem.
              • 19. Re: Traversing a 2d array...
                699554
                JoachimSauer wrote:
                mindloss wrote:
                When comparing to this...the only thing that is technically different is instead of array...I used array[0]....
                That's not the only difference.

                In this example i goes through different values in the first example and the second. The same is true for j.

                This is actually a nice example why you should give your variables names that tell you what they contain. I myself often fall in the trap of calling loop variables i and j. But if you called them currentRow and currentColumn in this example, then you would have quickly found the problem.
                True but he has accidently inverted the names lol, he has called rows - columns, and columns - rows. May not seem like anything to you but its really hard to understand myself.
                • 20. Re: Traversing a 2d array...
                  JoachimSauer
                  Melanie_Green wrote:
                  True but he has accidently inverted the names lol, he has called rows - columns, and columns - rows. May not seem like anything to you but its really hard to understand myself.
                  Oops. I didn't even realize that.

                  In a slightly attempt to try to make my overlooking it seem intentional: If you consequently replace column and row, then there's nothing wrong with that. It's all interpretation anyway. It's only customary that the first index chooses the row and the second one the column.
                  • 21. Re: Traversing a 2d array...
                    807588
                    Well thanks for the help guys. I can see where I messed up :).

                    You both were very helpful :).

                    Thanks
                    1 2 Previous Next