我的 2D 阵列已关闭,需要左下角为 (0,0)

My 2D Array is off, need the bottom left corner to be (0,0)

本文关键字:左下角 2D 我的 阵列      更新时间:2023-10-16

我的代码显示的数据网格

网格[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;
}