我的 2D 阵列已关闭,需要左下角为 (0,0)
My 2D Array is off, need the bottom left corner to be (0,0)
我的代码显示的数据网格
网格[1][1] = 3;
网格[2][1] = 3;
网格[4][5] = 3;
网格[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;
}
我在声明数组时尝试了许多变体,但它不会按我的预期工作!
你只是以错误的方式对待索引。
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;
}
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- C++动态安全 2D 交错阵列
- 如何在C++或OpenCV子矩阵中的2D子向量上使用OpenACC?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 我的 2D 阵列已关闭,需要左下角为 (0,0)