我正在为2D数组参数苦苦挣扎

问题描述 投票:0回答:1

我必须使用户定义的函数随机化2d矩阵的值,并让另一个用户定义的函数将其打印出来。我在参数方面遇到问题,其他地方都给我带来困惑/反对的答案。

我已经尝试过

-int antcolony[SIZE][SIZE]
-int antcolony[][SIZE]
-int antcolony[][]
-int antcolony[SIZE,SIZE]

和其他我不记得的。请帮助

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

/*  Function: colonizePhermones
* Parameters: int Size, const int antColony[][SIZE]
* Return: n/a
* Description: randomize the values in antColony
*/
void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]);

/*  Function: printPhermones
* Parameters: const int antColony[][SIZE]
* Return: n/a
* Description: print the values in antColony
*/
void printPhermones(int SIZE, int antColony[SIZE][SIZE]);



int main()
{
//declaring variables
const int SIZE = 10;
int colonyPath[SIZE];
int antColony[SIZE][SIZE];
int pathCounter = 0;
colonyPath[pathCounter]= 0;

//test caling functions
colonizePhermones(SIZE, antColony[SIZE][SIZE]);
printPhermones(SIZE, antColony[SIZE][SIZE]);

return 0;
}

void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]){
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++){
if ( i == j)
{
antColony[i][j] = 0;
}
else
{
antColony[i][j] = ((rand() % 19) +1);
}
}
}
}

void printPhermones(int SIZE, int antColony[SIZE][SIZE]){
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++)
{
cout << antColony[i][j] << " ";
}
cout << endl;
}
}

它什么也没做,只是给我错误信息。我需要它来打印数组。

c++
1个回答
0
投票

以下行无效:

void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]);

通常,通过具有数组的数组,C样式的数组可以是多维的。这意味着指向外部数组元素的指针将是指向数组的指针。

但是,为了声明指针,必须知道指针的确切类型,即指针指向的元素的类型。为了声明一个指向数组数据类型的指针,因此,您还必须指定指针指向的数组的大小。该大小必须是在编译时已知的恒定值。但是,在函数声明的情况下,它不是常数,因为您将数组的大小指定为第一个函数参数(“ SIZE”)的值。函数参数始终是变量,并且永远都不是编译时常量,因为编译器必须假定可以使用不同的参数来调用函数(即使程序中从未发生过)。

因此,为了修复您的程序,建议您将上面引用的行更改为以下内容:

void colonizePhermones(int array_size, int antColony[SIZE][SIZE]);

这样,第二个参数中数组的大小不再引用第一个参数。但是现在未声明变量SIZE。因此,我建议您现在移线

const int SIZE = 10;

从函数main到全局范围,使其成为全局变量。之后,数组的大小现在将是一个编译时常量,并且您将不再收到错误消息。

现在功能colonizePheromones已固定。要修复功能printPhermones,您必须执行相同的操作。

但是,您的程序中还有另一个错误。以下几行是错误的:

colonizePhermones(SIZE, antColony[SIZE][SIZE]);
printPhermones(SIZE, antColony[SIZE][SIZE]);

作为两个函数中的第二个参数,必须将一个指针传递给整个数组。相反,您传递的是单个元素的值。该元素甚至不存在,因为您正在访问数组。如果SIZE为10,那么您将通过以下方式访问数组:

antColony[10][10]

但是,有效索引为0-9,因为数组在两个维度上的大小均为10。

因此,我上面引用的两行应如下所示:

colonizePhermones(SIZE, antColony);
printPhermones(SIZE, antColony);

整个程序将看起来像这样,并且没有错误地进行编译:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 10;

/*  Function: colonizePhermones
* Parameters: int Size, const int antColony[][SIZE]
* Return: n/a
* Description: randomize the values in antColony
*/
void colonizePhermones( int array_size, int antColony[SIZE][SIZE] );

/*  Function: printPhermones
* Parameters: const int antColony[][SIZE]
* Return: n/a
* Description: print the values in antColony
*/
void printPhermones( int array_size, int antColony[SIZE][SIZE] );



int main()
{
    //declaring variables

    int colonyPath[SIZE];
    int antColony[SIZE][SIZE];
    int pathCounter = 0;
    colonyPath[pathCounter] = 0;

    //test caling functions
    colonizePhermones( SIZE, antColony );
    printPhermones( SIZE, antColony );

    return 0;
}

void colonizePhermones( int array_size, int antColony[SIZE][SIZE] ) {
    for ( int i = 0; i < SIZE; i++ ) {
        for ( int j = 0; j < SIZE; j++ ) {
            if ( i == j )
            {
                antColony[i][j] = 0;
            }
            else
            {
                antColony[i][j] = ((rand() % 19) + 1);
            }
        }
    }
}

void printPhermones( int array_size, int antColony[SIZE][SIZE] ) {
    for ( int i = 0; i < SIZE; i++ ) {
        for ( int j = 0; j < SIZE; j++ )
        {
            cout << antColony[i][j] << " ";
        }
        cout << endl;
    }
}

使用这些C样式数组的替代方法是使用std::vector,它的优点是长度不必是编译时常量,而是可以在运行时动态更改。

© www.soinside.com 2019 - 2024. All rights reserved.