Created
March 14, 2021 23:00
-
-
Save victorholt/78e3019a2e6d7a6c3d07d155c951caee to your computer and use it in GitHub Desktop.
Simple Array 1D to Array 2D and back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment