Reputation: 21
I just started learning C++. I learned Java before and I found that C++ is very similar in terms of structure.
Here is a 2D array and I traverse it and print all the values to make a 3 by 3 grid of 0's. But it prints out weird numbers:
#include <iostream>
using namespace std;
int main() {
int board[3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << board[i][j] << " ";
}
cout << "\n";
}
}
This prints out:
0 0 0
0 -13088 0
-13168 0 -12816
Edit: Thank you! I didn't know that C++ needed initialization. Arrays in Java are stored with 0's by default. Will keep this in mind while learning!
Upvotes: 0
Views: 197
Reputation: 111
initialization is missing
int board[3][3] = {{1,2,3},{1,2,3},{1,2,3}};
output
1 2 3
1 2 3
1 2 3
Upvotes: 2