我接到了一项作业,要求我编写两个 C 函数:
1 - Read a matrix from the user.
2 - Write a matrix to the console.
未指定矩阵维度,因此我无法使用以下内容:
void matwrite(float m[3][3]) { ... }
有什么办法可以解决这种情况吗?
最佳答案
函数签名应该类似于:
void matwrite(float** m, int w, int h);
然后你可以像这样迭代元素:
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
cout << m[i][j] << " "; // For example
}
cout << "\n";
}
您可以使用数组及其维度调用该函数,例如:
float arr[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{1, 2, 3}
};
matwrite(arr, 4, 3);
关于C 将二维数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26703635/