我的程序编译,所以没有错误,但肯定有一个错误的原因是返回什么。
我期望返回初始化板的最小值。代码在我看来是正确的,但我没有得到我想要的结果。关键是找到int
初始化板的最小数量
#include<stdio.h>
int find(int board[]);
int main (){
int board[6] = {6, 2, 6, 99, 0, 2};
find(board[6]); //call
printf(" %d",find(board[6]));
return 0;
}
int find(int board[]){
int minimum = board[0]; //initialize the first cell as the minimum one
for(int i = 0; i < 6; i++){
if(minimum > board[i]) minimum = board[i];
printf("%d",minimum);
}
return minimum;
}
字符串
2条答案
按热度按时间muk1a3rh1#
代码的某些部分需要修改:
board[6]
时,数组的名称是board
而不是board[6]
。因此,当将数组传递给函数时,我们应该使用board
作为参数。修改后的版本:
字符串
i86rm4rw2#
字符串
至少你应该在这里收到了一个警告,因为你将
find()
函数定义为一个接收六个整数数组作为参数的函数,并且你传递了一个整数(board[6]
只是数组最后一个元素之后的元素,这是你根本不能使用的,因为board
数组的元素从索引0到索引5,总共6个元素)你应该定义find()
来指示要使用的数组,该数组有多大(它有多少个元素,因为如果你只传递数组引用,函数将不知道这一点)以及你想要搜索的值,如:型
在
main()
中,将其用作:型
在您的例子中,当您搜索列表时,正确的方法应该是编写以下
find()
:型
然后,在main中:
型
这将产生:
型