1

I'm currently writing a C program that finds the elements that are larger than all of their neighbors in a multidimensional array. So if my input is

3 2 9

13 7 6

1 5 8

It should print

9
13
8

However, it doesn't seem to be working :( I've run through it a couple of times and can't seem to find anything wrong with it, other than the possibility that I'm handling my arrays incorrectly. Do you guys have any suggestions?

Here is my current code. http://pastebin.com/sJBhQMjy

Thank you!

PS: On my pastebin link I said "two-dimensional array"-- this was a typo, I just meant multidimensional.

1 Answer 1

1

Your program does not work because your widening has no effect. You are assigning a local array [n+2][n+2] to a pass-by-value parameter, so the change has no effect.

Even if it did, the loops in the main would explore the wrong part of the widened array (the upper-left corner, as opposed to the center, where the real data is stored).

Finally, your innermost if compares a[i][j] to a[m][n] instead of a[m][p].

Instead of widening the array, you should add more conditions to the innermost if of the neighbor function:

if(i>=0 && i<n && j>=0 && j<n && a[i][j]>a[m][p]) return FALSE;
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thank you very much! Those changes definitely made it work... And I guess it is rather efficent just to have those conditions in the if statement. Though, if we did want to use the "widen array" method and not have that, would it be possible? Can we make the size of an array larger?
@user1207214 It is not possible to change the size of an array in C. If you would like to use the approach with the "guard rows", you should allocate an array of [n+2][n+2] size, clear all its entries, and then use only its central portion in the initArray and neighbor functions.

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.