我的代码显示了一个数据网格
grid [1] [1] = 3;
grid [2] [1] = 3;
grid [4] [5] = 3;
grid [5] [6] = 3;
但我的显示变为
# # # # # # # # # # #
8 # #
7 # #
6 # #
5 # 3 #
4 # 3 #
3 # #
2 # 3 #
1 # 3 #
0 # #
# # # # # # # # # # #
0 1 2 3 4 5 6 7 8
这是错误的
有点不对劲,我无法使2D阵列正确对齐。 声明2D数组时,我需要左下角为(x,y)==(0,0)
我想要的是
# # # # # # # # # # #
8 # #
7 # #
6 # 3 #
5 # 3 #
4 # #
3 # #
2 # #
1 # 3 3 #
0 # #
# # # # # # # # # # #
0 1 2 3 4 5 6 7 8
#include <iostream>
#include <string>
using namespace std;
int ** grid = nullptr;
void initGrid(int col, int row);
void populateGrid(int col, int row);
void safelyDeallocateMemory (int col, int row);
int main() {
int row = 8
int col = 8
initGrid (col, row);
populateGrid(col, row);
safelyDeallocateMemory(col, row);
}
void initGrid (int col, int row) {
grid = new int * [col];
for (int i = 0; i < col; i++){
grid [i] = new int [row];
}
//Information should stream from text file
//test
grid[1][1] = 3;
grid[4][5] = 3;
grid[2][1] = 3;
grid[5][6] = 3;
}
void populateGrid(int col, int row) {
cout << " ";
//Top outer Grid
for (int i = 0; i < col + 2; i++) {
cout << " # ";
}
cout << " # ";
cout << endl;
//end
//y-axis
for (int i = row; i >= 0; i--) {
cout << " " << i << " # ";
//Number of Columns
for (int j = 0; j <= col; ++j) {
if (grid[i] == 0 || grid[j] == 0 ||grid[i][j] == 0) {
cout << " ";
}
else {
cout << grid[i][j] << " ";
}
}
//Right outer Grid
cout << " #";
cout << endl;
}
//Last row of #
cout << " ";
for (int i = 0; i < col + 2; i++) {
cout << " # ";
}
cout << " # ";
cout << endl;
cout << " ";
//x-Axis
for (int i = 0; i <= col; i++) {
cout << i << " ";
}
cout << endl;
}
我在声明数组时尝试了多种变体,但它无法按我的预期工作!
答案 0 :(得分:0)
您只是错误地对待索引。
grid[2][1] = 3;
与您的情况不同,grid[x][y] = 3;
与grid[y][x] = 3;
不同。
把它想象成一个数组数组。第一个索引描述数组在“数组数组”中的位置,第二个索引描述在该数组中的位置。
[0] -> [0] [1] [2] [3] [4] [5] <- row 0
[1] -> [0] [1] [2] [3] [4] [5] <- row 1
[2] -> [0] [1] [2] [3] [4] [5] <- row 2
[3] -> [0] [1] [2] [3] [4] [5] <- row 3
[4] -> [0] [1] [2] [3] [4] [5] <- row 4
[5] -> [0] [1] [2] [3] [4] [5] <- row 5
这取决于您如何处理这些索引。
获得所需结果的简单解决方案是通过以下方式更改void initGrid (int col, int row)
:
void initGrid (int col, int row) {
grid = new int * [row];
for (int i = 0; i < row; i++){
grid [i] = new int [col];
for(int j = 0; j < col; j++) {
grid[i][j] = 0;
}
}
//Information should stream from text file
//test
grid[1][1] = 3;
grid[5][4] = 3;
grid[1][2] = 3;
grid[6][5] = 3;
}
另外,您在使用void populateGrid(int col, int row)
时遇到了一些麻烦,您正试图访问for (int j = 0; j <= col; ++j)
中的超出范围的元素,因此您的代码应更像这样:
void populateGrid(int col, int row) {
cout << " ";
//Top outer Grid
for (int i = 0; i < col + 2; i++) {
cout << " # ";
}
//cout << " # ";
cout << endl;
//end
//y-axis
for (int i = row - 1; i >= 0; i--) {
cout << " " << i << " # ";
//Number of Columns
for (int j = 0; j < col; ++j) {
if (grid[i][j] == 0) {
cout << " ";
}
else {
cout << grid[i][j] << " ";
}
}
//Right outer Grid
cout << "#";
cout << endl;
}
//Last row of #
cout << " ";
for (int i = 0; i < col + 2; i++) {
cout << " # ";
}
cout << endl;
cout << " ";
//x-Axis
for (int i = 0; i < col; i++) {
cout << i << " ";
}
cout << endl;
}