gpt4 book ai didi

c++ - 将值输入二维数组并打印

转载 作者:行者123 更新时间:2023-11-28 05:00:03 26 4
gpt4 key购买 nike

我正在尝试编写一个 C++ 程序,该程序请求 3 x 3 数组的输入,然后打印它们。我正在尝试打印它,但不确定为什么每个值都给我 0

#include <iostream>

using namespace std;

const int row = 3;
const int col = 3;

void printMatrix(int array[row][col])
{
int i, j;
cout << endl << "Matrix " << endl;

for(i = 0; i < row; i++)
{
cout << endl;
for(j = 0; j < col; j++)
{
cout << array[row][col] << "\t";
}
}

cout << "\n";
}

int main()
{
int i, j, array[row][col];

for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << "Enter a value for Row " << i + 1 << " Col " << j + 1 << ": "
cin >> array[i][j];
}
}

printMatrix(array);

}

控制台输出:

Enter a value for Row 1 Col 1: 1

Enter a value for Row 1 Col 2: 2

Enter a value for Row 1 Col 3: 3

Enter a value for Row 2 Col 1: 1

Enter a value for Row 2 Col 2: 2

Enter a value for Row 2 Col 3: 3

Enter a value for Row 3 Col 1: 1

Enter a value for Row 3 Col 2: 2

Enter a value for Row 3 Col 3: 3

输出矩阵:

0       0       0

0 0 0

0 0 0

最佳答案

问题就在这里

cout << array[row][col] << "\t";

尝试

cout << array[i][j] << "\t";

关于c++ - 将值输入二维数组并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com