C语言 我的程序在编译时没有返回任何东西,我找不到错误,看起来代码很好

xmjla07d  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(95)

我的程序编译,所以没有错误,但肯定有一个错误的原因是返回什么。
我期望返回初始化板的最小值。代码在我看来是正确的,但我没有得到我想要的结果。关键是找到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;
}

字符串

muk1a3rh

muk1a3rh1#

代码的某些部分需要修改:

  • 当一个数组被声明为board[6]时,数组的名称是board而不是board[6]。因此,当将数组传递给函数时,我们应该使用board作为参数。
  • 如果我们想使用函数的返回值,我们应该将返回值赋给一个变量,这样我们以后就可以使用它。

修改后的版本:

#include <stdio.h>

int find(int board[]);

int main() {
    int board[6] = {7, 5, 8, 2, 4, 1};
    int minimum = find(board);
    printf("%d\n", minimum);
    return 0;
}

int find(int board[]){
    int minimum = board[0];
    for (int i = 0; i < 6; i++) {
        printf("%d\n", minimum);
        if (minimum > board[i]) {
            minimum = board[i];
        }
    }
    return minimum;
}

字符串

i86rm4rw

i86rm4rw2#

#include<stdio.h>

int find(int board[]);

int main (){
    int board[6] = {6, 2, 6, 99, 0, 2};
    find(board[6]); //call

字符串
至少你应该在这里收到了一个警告,因为你将find()函数定义为一个接收六个整数数组作为参数的函数,并且你传递了一个整数(board[6]只是数组最后一个元素之后的元素,这是你根本不能使用的,因为board数组的元素从索引0到索引5,总共6个元素)你应该定义find()来指示要使用的数组,该数组有多大(它有多少个元素,因为如果你只传递数组引用,函数将不知道这一点)以及你想要搜索的值,如:

int find(int array[], size_t array_len, int value_to_search)
{
    /* blah blah */
    return /* the index of the found element */;
}


main()中,将其用作:

int found = find(board, 6, 99);  /* search array board, of six elements
                                      * for element whose value is 99
                                      * and return the index at which
                                      * it was found, or BAD in case
                                      * that element was not found */
    if (found == BAD) { /* BAD should be a constant you will never produce, like -1 or the size of the array, as that number will never be accessible */
         /* blah blah error */
        return 1;
    }
    printf("element 99 was found at position %d in array board\n", found);
    return 0;
}


在您的例子中,当您搜索列表时,正确的方法应该是编写以下find()

#define BAD (-1)

int find(int array[], size_t array_len, int value_to_search)
{
    for (size_t i = 0; i < array_len; i++)
         if (array[i] == value_to_search)
             return i;
    /* if we get here, we have not found it */
    return BAD;
} /* find */


然后,在main中:

#include <stdio.h>
int find(int board[], size_t board_size, int to_search);

int main()
{
      int board[6] = {6, 2, 6, 99, 0, 2};
      int found = find(board, 6, 99);
      int to_search[3] = {99, 6, 100};

      for (int i = 0; i < 3; i++) {
         int found = find(board, 6, to_search[i]);
         if (found == BAD) {
             printf("value %d not found in array\n", to_search[i]);
         } else { /* found */
             printf("value %d found at position %d\n",
                     to_search[i], found);
         }
      }
      return 0;
} /* main */


这将产生:

$ a.out
value 99 found at position 3
value 6 found at position 0
value 100 not found in array

相关问题