我正在为2D阵列参数而挣扎

I am struggling with 2D array parameters

本文关键字:参数 挣扎 阵列 2D      更新时间:2023-10-16

我必须制作用户定义的函数来随机化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. 通过引用
  2. 传递指针

您似乎想通过引用传递。但是你使用了错误的语法。

请参阅:

void function1(int(&m)[3][4])   // For passing array by reference
{}
void function2(int(*m)[3][4])   // For passing array by pointer
{}
int main()
{
int matrix[3][4]; // Define 2 dimensional array
function1(matrix);  // Call by reference
function2(&matrix); // Call via pointer 
return 0;
}

传递给函数的是指向int.数组的衰减指针

只要纠正语法就行了。

附加提示:

不要在C++中使用纯C样式数组。从不请使用STL容器。

以下行无效:

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,其优点是长度不必是编译时常数,而是可以在运行时动态更改。