- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须让用户定义的函数随机化二维矩阵的值,并让另一个用户定义的函数打印出来。我在参数方面遇到问题,其他地方都给我令人困惑/相反的答案。
我试过了
-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 数组传递给一个函数,您有 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容器。
关于c++ - 我正在努力处理二维数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584057/
我正在尝试为我的项目创建一些单元测试,经过大量挖掘之后,我发现了Effort,这个想法很棒,它模拟数据库而不是处理伪造的DBContext,顺便说一句,很难做到正确使用复杂的架构。 但是,我将用户的电
我正在尝试对某些响应 Entity Framework 数据库上下文的类进行单元测试。为了寻求帮助,我设法找到了一个名为 Effort 的库,它似乎有点旧,而且没有很好的文档记录,但它似乎可以工作,而
这两天,Auto-GPT 爆火 https://github.com/Torantulino/Auto-GPT 它是一款让最强语言模型GPT-4能够自主完成任务的模型,让整个AI圈疯
为什么会出现这个异常?这是错误吗? 我正在使用 EF 测试库 Effort 创建我的数据库的内存实例并遇到这个有趣的场景: 打开DbContext1 将项目添加到表(不保存) 关闭DbContext1
我是一名优秀的程序员,十分优秀!