0

I want to search a multi-dimensional array and print the numbers greater than 7 with their locations.

This code compiles and runs without any errors, but doesn't provide any output.

Please help me to solve this problem.

class Sarr{

   public static void main(String args[]){    
     int[][] numArray = {{1,2,5,6,4,0},{6,0,1,2},{1,7,3,4},{3,5,6,8,5}};      
     arr(numArray);
   }

   private static void arr(int [][] array){

   int val = 7;

   for (int r = 0; r < array.length; r++) {
        for (int c = 0; c < array[r].length; c++) {

          if (array[r][c] > val){

             System.out.println("Value found was " + val + "["+r+"]"+"["+c+"]");

           }
        }            
    }
  }    
}
3
  • Ok, so what does it output now that you added an 8? Commented Mar 9, 2011 at 14:45
  • you edit the test array, which you pass as a reference, on my machine it o/p as Value found was 7[3][3] Commented Mar 9, 2011 at 14:48
  • tnx all... i totally forgot to add a value greater than 7, to my array. i edit it and compiled it. now it works. Commented Mar 9, 2011 at 14:59

3 Answers 3

7

Your test array does not have any element which is > 7 ...

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that there is no number greater than 7 in your array. If you want it to print 7's you will need to change your if statement to

if(array[r][c]>=val) {
    //Print
}

Comments

1

It's because you're looking for strictly array[r][c] > 7 none of the values in your array are greater than 7.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.