0

I am working with a 2D array and I would like to view the elements to the "top left" "top" and "top right" of the smallest element in the last row. I have a code that works however it displays all of the directions of every element in the row, not just the smallest one. Can anyone help? This is my code:

for (int y = array.length-1; y == array.length-1; y--)
{
    for (int x = 0; x < array[y].length; x++)
    {
        int lowest = array[y][0];  
        for (x = 0; x < array[y].length; x++)
        {
            if (array[y][x] <= lowest)
                lowest = array[y][x];

            //if element is on left
            if (x == 0)
            {
                up = array[y-1][x];
                upRight = array[y-1][x+1];
                upLeft = 0;
            }

            //if element is on right
            else if (x == array[0].length - 1)
            {
                upLeft = array[y-1][x-1];
                up = array[y-1][x];
                upRight = 0;
            }

            //if element is anywhere else
            else
            {
                upLeft = array[y-1][x-1];
                up = array[y-1][x];
                upRight = array[y-1][x+1];
            }
        }
    }

}

2
  • Show an example of the data in the array...what do you mean by "top" etc Commented Nov 9, 2012 at 15:16
  • Ok so if I had an array: {{1 3 4 5}, {3 7 8 2}, {4 5 2 7}} The lowest value in the last row is 2, upLeft should be 7 up should be 8 and upRight should be 2. If either upLeft or upRight is out of bounds it sets which ever one to 0 Commented Nov 9, 2012 at 15:25

1 Answer 1

1

A couple of observations.

It seems like at the moment you have 2 nested for loops where you're iterating over the whole array. From your description it sounds like this isn't necessary and you can just search array[array.length - 1] for the lowest value.

Concentrate on writing a separate step (possibly its own method) that will find the index of the lowest value on the last row.

You pretty much have the logic correct for finding top left, top and top right. You just need it to look at elements from array[array.length - 2] based on the index of the lowest value from the last row.


Update:

You mentioned in the question that your code "displays all of the directions of every element in the row". You haven't included any of the code that displays the results but from your comment below it sounds like your problem is as follows:

Your code that sets upLeft, up and upRight is inside the loop that is searching for the lowest value. i.e. you have:

for (x = 0; x < array[y].length; x++)
        {
            if (array[y][x] <= lowest)
                lowest = array[y][x];

            //if element is on left
            if (x == 0)

so this means that it happens for every element in the row.

What you probably want to do is have your loop to find the lowest element, and also remember its index:

for (x = 0; x < array[y].length; x++)
{
    if (array[y][x] <= lowest) {
        lowest = array[y][x];
        indexOfLowest = x;
    }
}

and then after that, outside of the inner loop, set upLeft, up and upRight using the index of the lowest element found:

//if element is on left
if (indexOfLowest == 0)

etc.

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

4 Comments

The reason I have a nested for loop accounting for the whole array is because I need to find the smallest value "above" the last element and then do the same thing until I reach the top of the array. I failed to mention that but it is not a step I'm concentrating on at the moment
OK, I've updated the answer with a few more suggestions. Hope it points you in the right direction.
If the variable indexOfLowest is in the inner loop but the rest of the code is outside of the inner loop how does indexOfLowest = x; get set to if(indexOfLowest ==0) ?
Hi. As you've marked the answer as accepted have you figured this out now? If not, as a suggestion I'd declare and initialise int indexOfLowest = 0 right after you're currently doing int lowest = array[y][0];

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.