[英]neighbors.c program debugging (multidimensional arrays)
I'm currently writing a C program that finds the elements that are larger than all of their neighbors in a multidimensional array. 我目前正在编写一个C程序,该程序查找比多维数组中所有相邻元素大的元素。 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
http://pastebin.com/sJBhQMjy
Thank you! 谢谢!
PS: On my pastebin link I said "two-dimensional array"-- this was a typo, I just meant multidimensional. PS:在我的pastebin链接上,我说“二维数组”-这是一个错字,我只是说多维。
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. 您正在将本地数组
[n+2][n+2]
分配给传递值参数,因此更改无效。
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). 即使这样做,
main
循环也将探索加宽数组的错误部分(左上角,而不是中心,存储实际数据)。
Finally, your innermost if
compares a[i][j]
to a[m][n]
instead of a[m][p]
. 最后,最里面的
if
将a[i][j]
与a[m][n]
而不是a[m][p]
。
Instead of widening the array, you should add more conditions to the innermost if
of the neighbor
function: 相反,拓宽阵列的,你应该增加更多的条件到最里面
if
在的neighbor
功能:
if(i>=0 && i<n && j>=0 && j<n && a[i][j]>a[m][p]) return FALSE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.