在2d数组中输入值并打印

时间:2017-09-17 14:46:57

标签: c++ arrays pointers matrix

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

#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);

}

控制台输出:

  

输入第1行Col 1:1

的值      

输入第1行Col 2:2

的值      

输入第1行Col 3:3

的值      

输入第2行Col 1:1

的值      

输入第2行Col 2:2

的值      

输入第2行Col 3:3

的值      

输入第3行Col 1:1

的值      

输入第3行Col 2:2的值

     

输入第3行Col 3:3

的值

输出矩阵:

0       0       0

0       0       0

0       0       0

2 个答案:

答案 0 :(得分:1)

问题出在这里

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

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

答案 1 :(得分:0)

您只需要更改z.extractall(outDir, pwd=b'1234!') 功能。 更改此功能如下:

printMatrix()

并在主函数的cout << array[i][j] << "\t"; 语句末尾加上分号(;)。

相关问题