Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将值从一个函数传递到另一个C++;_C++_Function Declaration - Fatal编程技术网

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

C++ 将值从一个函数传递到另一个C++;,c++,function-declaration,C++,Function Declaration,我在写两个函数:其中一个用于用随机值“填充”数组,而int第二个函数我必须使用相同的数组,选择一行并找到该行的min元素 但问题是我不知道如何将值从一个函数传递到另一个函数 这是我的密码: #include <cstdlib> #include <ctime> #include <iostream> using namespace std; void fillarray(int arr[5][5], int rows, int cols) { cou

我在写两个函数:其中一个用于用随机值“填充”数组,而int第二个函数我必须使用相同的数组,选择一行并找到该行的min元素

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

这是我的密码:

#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");
}
#包括
#包括
#包括
使用名称空间std;
空填充数组(int arr[5][5],int行,int列){

cout对于初学者,函数
fillarray
具有冗余参数
cols
,因为从第一个参数
int arr[5][5]
的声明中可以知道该数字

Th函数可以声明为

void fillarray(int arr[5][5], int rows )
void minarray( const int arr[][5], size_t n, size_t row );
如果函数中未填充整个数组,则可以提供参数
cols

您已通过此调用填充了数组

fillarray ( arr, rows, cols );
函数执行了它的任务。因此,在尝试时不需要再次引用函数

minarray(z, fillarray)
函数
minarray
可以这样声明

void minarray( const int arr[], size_t n );
打电话给我

minarray( arr[z], cols );
minarray( arr, rows, z );
初步检查z是否小于5

或者可以这样声明

void fillarray(int arr[5][5], int rows )
void minarray( const int arr[][5], size_t n, size_t row );
打电话给我

minarray( arr[z], cols );
minarray( arr, rows, z );
请注意,有标准算法
std::min_element
允许查找数组中的最小元素。要用值填充数组,可以使用标准算法
std::generate


每个函数应该只执行一个任务。例如,函数
fillarray
应该用值悄悄地填充数组。要输出数组,可以编写一个单独的函数。

对于初学者,函数
fillarray
具有冗余参数
cols
,因为这个数字是从第一个参数
int-arr[5][5]

Th函数可以声明为

void fillarray(int arr[5][5], int rows )
void minarray( const int arr[][5], size_t n, size_t row );
如果函数中未填充整个数组,则可以提供参数
cols

您已通过此调用填充了数组

fillarray ( arr, rows, cols );
函数执行了它的任务。因此,在尝试时不需要再次引用函数

minarray(z, fillarray)
函数
minarray
可以这样声明

void minarray( const int arr[], size_t n );
打电话给我

minarray( arr[z], cols );
minarray( arr, rows, z );
初步检查z是否小于5

或者可以这样声明

void fillarray(int arr[5][5], int rows )
void minarray( const int arr[][5], size_t n, size_t row );
打电话给我

minarray( arr[z], cols );
minarray( arr, rows, z );
请注意,有标准算法
std::min_element
允许查找数组中的最小元素。要用值填充数组,可以使用标准算法
std::generate


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

我不确定这是否可以编译,但我猜您希望传递int arr[x][y]从fill数组函数到minArray函数。要做到这一点,您首先需要将arr作为minArray的一个参数包含进来。从那里您需要通过引用传递它。然后,您可以从fillArray调用minArray。

我不确定这是否可以编译,但我猜您希望传递int arr[x][y]从fill数组函数到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];
    }
}
定义填充数组:

void fillarray(int arr[rows][cols]) {
当您从main调用它时:

fillarray(arr);

您需要做的是调用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];
    }
}
定义填充数组:

void fillarray(int arr[rows][cols]) {
当您从main调用它时:

fillarray(arr);

我将让其他答案回答您的问题,并将注意力集中在您在评论中询问的
goto
周围的代码上

main
中,您有:

    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 
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
周围的代码上

main
中,您有:

    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 
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;
    }

void fillarray
?您想要什么?是否要将arr从main传递到
minarray()
注意:在
minarray(z,fillarray)之后缺少一个
。这无法编译。
转到那里;
?您是否真正了解了作为第一件事之一的
goto
?在
minarray
的范围内,函数不知道
arr
是什么。它不能仅仅从main中提取
arr
,您需要显式地将其发送到数组,就像将
arr
发送到
fillarray
@jane太糟糕了。暂时忘记你知道的
goto
。总有™