所以我不确定我做错了什么。我知道这将是一件小而简单的事,但我确实需要一点帮助。它只打印出大部分每行的第一个数字,之后它只打印出0。可能只是我整天盯着电脑而且压力导致我的大脑功能不好但任何帮助都会很棒。我知道大多数人都使用i和j,但我喜欢使用r来表示列的行和c。
#include <iostream>
using namespace std;
int main() {
int m1[4][4] = {(1, 2, 3, 4), (4, 3, 2, 1), (1, 2, 3, 4), (4, 3, 2, 1)};
int m2[4][4] = {(0, 0, 0, 0), (1, 1, 1, 1), (2, 2, 2, 2), (3, 3, 3, 3)};
int m3[4][4] = {(5, 8, 2, 5), (4, 7, 1, 4), (6, 9, 3, 6), (4, 5, 6, 4)};
int m4[4][4] = {(1, 3, 7, 9), (2, 6, 8, 4), (7, 8, 9, 6), (1, 2, 3, 4)};
//print matrix 1
cout << "Matrix 1: " << endl;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
cout << m1[r][c] << " ";
}
cout << endl;
}
cout << endl << endl;
//print natrix 2
cout << "Matrix 2: " << endl;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
cout << m2[r][c] << " ";
}
cout << endl;
}
cout << endl << endl;
//print matrix 3
cout << "Matrix 3: " << endl;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
cout << m3[r][c] << " ";
}
cout << endl;
}
cout << endl << endl;
//print matrix 4
cout << "Matrix 4: " << endl;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
cout << m4[r][c] << " ";
}
cout << endl;
}
cout << endl << endl;
cout << endl << endl;
system("pause");
}
答案 0 :(得分:3)
使用大括号{}
代替括号()
。使用括号,您只需使用逗号分隔的表达式,该表达式的计算结果为最右边的数字(因此仅初始化第一行),其余元素为default initialized到0
。你想要的是list initialization。而不是:
int m1[4][4] = { (1, 2, 3, 4), (4, 3, 2, 1), (1, 2, 3, 4), (4, 3, 2, 1) };
使用:
int m1[4][4] = { { 1, 2, 3, 4 }, { 4, 3, 2, 1 }, { 1, 2, 3, 4 }, { 4, 3, 2, 1 } };
并重复剩余的矩阵。