Created
September 28, 2012 02:45
-
-
Save emonti/3797663 to your computer and use it in GitHub Desktop.
2d grid example for malic
This file contains 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 <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