0

I have a 2^n size int array and I want to check if an element exists that is greater than 0. If the element exists, I want to divide the array by 4 and check if the coordinates of the found element are in the 1st, 2nd, 3rd or 4th quadrant of the array.

For example, logically if the element exists in the first quadrant it would look something like this:

If array[][] > 0 && the row of that coordinate is in the range 0-(grid.length/2-1) && the column of that coordinate is in the range 0-(grid.length/2-1) then do something.

I'm really not sure how to check the row and column index of the found element and store those coordinates to use in my if statement. Help!

3 Answers 3

1

your code should be look like this

for(int i = 0; i < array.length; i++){
    for(int j; j < array[i].length; j++){
        if(array[i][j] > 0){
           do some thing
         }
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're using a nested for loop, right? And, I'm guessing, you have a something like a function which returns the found element? So you need a function which returns the found element and its coordinates - or just the coordinates, if the array is available outside of this function.

Something like this (pseudocode):

for i from 0 to max X
    for j from 0 to max Y
        if array[i][j] > 0
            return (array[i][j], i, j) # A tuple, or whatever -
                                       # just some data structure for
                                       # storing multiple things

Comments

0

As I understand your question, you have the following case: Given an integer k between 0, N (representing the position of an element in a single dim. array) find the coordinates of the corresponding cell in 2-d array, i.e find x(i,j) where x has R rows and C columns.

Example (rows R=3 and Columns C=4)

0 1 2  3 
4 5 6  7
8 9 10 11

Given k=6  Then i=1, j=2
Given k=11 Then i=2, j=3

Given k, you can find i, j as follows:

i=Int(k/c) //Row number (0-based)

j=k%c // Column number - (0-based) obtained by the remainder of dividing k by c

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.