Skip to content

Instantly share code, notes, and snippets.

@emonti emonti/2dgrid.c
Created Sep 28, 2012

Embed
What would you like to do?
2d grid example for malic
#include <stdio.h>
#define ROWS 10
#define COLUMNS 7
// This is a 2-dimensional array.
// It makes accessing the values of a
// bitmap easy by using x/y references.
int grid[ROWS][COLUMNS] = {
{0,0,0,0,0,0,0}, // 7 columns across
{0,1,1,0,1,1,0},
{0,1,1,0,1,1,0},
{0,0,0,0,0,0,0},
{0,0,0,1,0,0,0},
{0,0,0,1,0,0,0},
{0,0,0,0,0,0,0},
{0,1,1,1,1,1,0},
{0,0,1,1,1,0,0},
{0,0,0,0,0,0,0},
// 10 columns down
};
int main() {
int x,y;
for(x=0; x < ROWS; x++) {
for(y=0; y < COLUMNS; y++) {
// to access a value in the bitmap we just reference it with our
// x/y coordinate.
int value = grid[x][y];
if (value == 1) {
putc('*', stdout); // output a single *
} else {
putc(' ', stdout); // or an empty space
}
}
putc('\n', stdout); // print end of line (and begin at next one)
}
}
/*
here's what the output looks like
$ ./2dgrid
** **
** **
*
*
*****
***
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.