Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    - Urgent deadline tomorrow! Problem with variable scope.

    I'm having an issue with this block of code primarily the columnIndex variable. The main reason Posting is I'm at wits end. The variable is clearly assigned a value within the bottom for loop but when I test the code it remains zero as initialized.
    The most fustrating part is that there is an alsomt identical method in my code that is not haveing the same problem.


    Code:
            public int findFirstCol(int[][] matrix){
               int rowColSize = matrix.length;
               int columnIndex = 0;
               int countOnes;
               int[] colOnesCount = new int[rowColSize];
                
                
                for (int p = 0; p < rowColSize; p++){
                     countOnes = 0;
                    for (int a = 0; a < rowColSize; a++){
                        //notice the switch in row vs col index variables
                        //and the move in position of the countOnes rest to zero.
                        if (matrix[a][p] == 1){
                            countOnes++;
                            colOnesCount[p] = countOnes;
                        }//end of if
                    }//end of nested for
                }//end of for;
               
                for (int z = 0; z < rowColSize-1; ++z){
                    if (colOnesCount[z] > colOnesCount[z+1]){
                        columnIndex = z;
                    }//end of if
                }//end of for loop
                
                return columnIndex;
            }//end of findFirstCol
    Any help will be appricated.

  2. #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,669
    Thanks
    58
    Thanked 663 Times in 658 Posts
    The obvious possibility would be that your if condition is never satisfied (or only satisfied once). Have you logged the results to see what is happening within that block?
    The other thing to note is that your code appears to be java while this branch of the forum is devoted to javascript

  3. #3
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,822
    Thanks
    6
    Thanked 1,292 Times in 1,262 Posts
    Moved to the Java subforum.


 

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
  •