Skip to content

Instantly share code, notes, and snippets.

@emonti
Created September 28, 2012 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emonti/3797685 to your computer and use it in GitHub Desktop.
Save emonti/3797685 to your computer and use it in GitHub Desktop.
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
Copy link
Author

emonti 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