将值从一个函数传递给另一个 C++

Passing value from one function to another C++

我正在编写两个函数:其中一个用于 "filling" 具有随机值的数组和 int 第二个函数我必须使用相同的数组,选择一行并找到该行的最小元素.

但问题是我不知道如何将值从一个函数传递到另一个函数。

这是我的代码:

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

void fillarray(int arr[5][5], int rows, int cols) {
    cout << "Static Array elements = \n\n" << flush;

    for(int i = 0; i < rows; ++i) {
        cout << "Row " << i << "  ";
        for(int j = 0; j < cols; ++j) {
            arr[i][j] = rand() % 10;
            cout << arr[i][j] << " " << flush;
        }
        cout << endl;
    }
    cout << " \n\n";
}

void minarray(int a, void fillarray) { // don't know what to write here

there:
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    if(a > 4) {
        cout << "Invalid input! " << endl;
        goto there;
    }

    for(int counter = 0; counter < 5; ++counter) {
        if(arr[a][counter] < min) min = arr[a][counter];
    }
    cout << "Minimum element is " << min << endl;
}
int main() {
    int z;

    srand(time(NULL));
    const int rows = 5;
    const int cols = 5;
    int arr[rows][cols];
    fillarray(arr, rows, cols);
    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 
    system("PAUSE");
}

对于初学者来说,函数 fillarray 有多余的参数 cols 因为这个数字是从第一个参数 int arr[5][5].

的声明中得知的

函数可以这样声明

void fillarray(int arr[5][5], int rows )

您可以提供参数 cols 以防函数中没有填充整个数组。

你已经通过这个调用填充了数组

fillarray ( arr, rows, cols );

函数执行了它的任务。因此,您在尝试时无需再引用该函数一次

minarray(z, fillarray)

函数 minarray 可以声明为

void minarray( const int arr[], size_t n );

并称赞

minarray( arr[z], cols );

初步检查 z 小于 5。

或者可以这样声明

void minarray( const int arr[][5], size_t n, size_t row );

并称赞

minarray( arr, rows, z );

注意标准算法 std::min_element 可以找到数组中的最小元素。要用值填充数组,您可以使用标准算法 std::generate.

而且每个函数应该只做一个任务。例如,函数 fillarray 应该默默地用值填充数组。要输出数组,您可以编写一个单独的函数。

我不确定这是否可以编译,但我猜您想将 int arr[x][y] 从 fill Array 函数传递给 minArray 函数。为此,您首先需要将 arr 作为 minArray 的参数。从那里你需要通过引用传递它。然后,您可以从 fillArray 调用 minArray。

您需要做的是调用 fillarray 来填充您的数组。所以它看起来像

fillarray(arr, rows, cols);

就像你到目前为止所做的那样。现在,你已经填满了数组 arr。minarray 不关心这是怎么发生的。所以不要将它传递给你的填充方法。将数组传递给它。

minarray(cols, arr[z]);

您不需要传递整个数组——只需传递有问题的行。你也传递了宽度。

并修改minarray的定义:

void minarray(int length, int[] array)

现在,您的 minarray 本身需要更改。首先,摆脱 if 检查。您现在不需要传递行号,但您确实需要传递列数作为长度。

然后你的 for 循环看起来像:

for (int index = 0; index < length; ++index) {
    if (array[index] < min) {
        min = array[index];
    }
}

所以,总结一下:

  • Main 声明数据并调用你的两个方法。

  • fillarray 填充数组。它以您已有的方式从 main 调用。

  • minarray 在单行上打印最小值。也是从main调用的,传入的是数组,不是填充的方法

但是,您还有一个问题。 fillarray 将数组大小硬编码为 5x5,但 main 使用定义的常量。我会将这些内容移动到文件的顶部并在这两个地方使用它们。

移到顶部,在任何 #includes 下方:

const int rows = 5;
const int cols = 5;

定义填充数组:

void fillarray(int arr[rows][cols]) {

当你从 main 调用它时:

fillarray(arr);

我会让其他答案回答您的问题,并专注于您在评论中询问的 goto 周围的代码。

main 你有这个:

    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 

minarray 你有这个:

void minarray(int a, void fillarray) { // don't know what to write here

there:
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    if(a > 4) {
        cout << "Invalid input! " << endl;
        goto there;
    }

首先,绝对没有理由使用 goto。你可以这样做:

void minarray(int a, void fillarray) { // don't know what to write here
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    while(a > 4) { // loop for as long as "a > 4"
        cout << "Invalid input! " << endl;
    }

删除 goto 使错误变得相当明显。 a 永远不会在循环内改变,所以如果你给它无效的输入,它只会永远打印 Invalid input! 。另一种方法是在您实际从用户那里获得输入时验证输入(在 main 中):

    while(true) { // loop forever
        cout << "Enter the number of row: ";
        if(cin >> z) {                  // check that the user inputs an int 
            if(z<0 || z>4)              // validate the input
                cout << "Invalid input!\n";
            else
                break;                  // we got valid input, break out of the while loop
        } else {                        // user did not input an int
            std::cout << "input failed - aborting\n";
            return 1;                   // return from main to exit the program
        }
    } // if the program reaches this point, it'll ask the user for input again
      // and that will only happen if the user gives it an int that is <0 or >4