Skip to content

Instantly share code, notes, and snippets.

@victorholt
Created March 14, 2021 23:00
Show Gist options
  • Save victorholt/78e3019a2e6d7a6c3d07d155c951caee to your computer and use it in GitHub Desktop.
Save victorholt/78e3019a2e6d7a6c3d07d155c951caee to your computer and use it in GitHub Desktop.

Revisions

  1. victorholt created this gist Mar 14, 2021.
    41 changes: 41 additions & 0 deletions array1d_array_2d_example.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #include <iostream>
    #include <string>
    #include <vector>

    static std::vector<int> map;
    static int mapSize = 3;

    int initMap()
    {
    for (int i = 0; i < (mapSize * mapSize); i++) {
    map.push_back(0);
    }
    }

    int setMapValue(int index, int value)
    {
    auto nx = index % mapSize;
    auto ny = index / mapSize;
    auto mIndex = nx + ny * mapSize;
    map[mIndex] = value;

    std::cout << "Setting Map Data at (" << nx << ", " << ny << ")" << std::endl;
    }

    void printMap()
    {
    for (int y = 0; y < mapSize; y++) {
    std::cout << "[ ";
    for (int x = 0; x < mapSize; x++) {
    std::cout << map[x + y * mapSize] << " ";
    }
    std::cout << "]" << std::endl;
    }
    }

    int main()
    {
    initMap();
    setMapValue(0, 5);
    printMap();
    }