Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
1d grid example for malic
#include <stdio.h>
int main()
{
// Notice, there are no brackets around the rows this time.
// This is a 1-dimensional array. Even though it looks 2d in
// the code, it's one long list to the computer.
//
// Using a 1-dimensional array, we can still treat the data
// inside of it as a grid in our code, though.
int flat_grid[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 14 rows
0,0,0,0,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,1,0,0,1,1,1,0,0,1,0,
0,0,0,0,1,0,1,1,1,1,0,1,1,0,
0,0,0,0,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,1,1,1,1,0,1,1,1,1,0,
0,0,0,0,1,1,1,1,1,0,1,1,1,0,
0,0,0,0,1,1,1,1,0,0,1,1,1,0,
0,0,0,0,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,1,1,1,1,0,0,1,1,1,0,
0,0,0,0,1,1,1,1,0,0,1,1,1,0,
0,0,0,0,0,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,
0,0,0,1,1,0,0,0,1,0,0,0,1,1,
0,0,0,0,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,
// to 17 columns
};
// lets set up some variables we'll need...
int columns, x, // no. of "columns", and our index into them is "x"
rows, y, // no. of "rows", and our index into them is "y"
position, // a variable to hold our "position" in the grid
value; // a variable to hold the "value" at the "position"
rows = 14;
columns = 17;
for(x=0; x < columns; x++) {
for(y=0; y < rows; y++) {
// We don't have a convenient 2-d array this time, but thats ok!
// We just need to calculate the position with a little "math"...
position = (x * rows) + y;
//... and get the value at that position!
value = flat_grid[position];
// the rest looks pretty much the same as before...
if (value == 1) {
putc('@', stdout);
} else {
putc(' ', stdout);
}
}
putc('\n', stdout); // end of line
}
}
/*
here's what it looks like when i run it:
$ ./1dgrid
@@@@@@@@@
@ @@@ @
@ @@@@ @@
@@@@@@@@@
@@@@ @@@@
@@@@@ @@@
@@@@ @@@
@@@@@@@@@
@@@@ @@@
@@@@ @@@
@@@@@@@
@
@@ @ @@
@@@@@@@@@
@@@
@
*/
@emonti

This comment has been minimized.

Copy link
Owner Author

commented Sep 28, 2012

boo, it looks better on a black+white background :)

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.