#include #include // exit(int) is in stdlib void main() { FILE * f = fopen("mystery.dat", "r"); if (f == NULL) { printf("file not found\n"); exit(1); } // Mistake warning: // fseek returning the new position is not standard // after all (I thought it was). To find the length, you // have to fseek the end then get the answer from ftell. fseek(f, 0, SEEK_END); int filesize = ftell(f); fseek(f, 0, SEEK_SET); const int columns = 800; const int rowsize = columns * sizeof(short int); int rows = filesize/rowsize; // This is how you create a true two dimensional array dynamically. // rows is allowed to be a variable, but columns must be a constant. short int (* a)[columns]; a = new short int[rows][columns]; int n = fread(a, rowsize, rows, f); printf("%d rows read\n", rows-1); for (int row = 1; row < rows; row += 10) { for (int col = 0; col < columns; col += 10) { int watercount = 0; for (int i = 0; i < 10; i += 1) for (int j = 0; j < 10; j += 1) if (a[row+i][col+j] == -500) watercount += 1; if (watercount == 100) putchar('#'); else if (watercount > 50) putchar('O'); else putchar(' '); } putchar('\n'); } }