#include #include #include //for strcmp; if you don't have it, try string.h using namespace std; int main() { const size_t life_ymax = 10; const size_t life_xmax = 10; bool oldmatrix[life_ymax][life_xmax] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; for (int generation = 0;; ++generation) { //Print the oldmatrix. for (size_t y = 0; y < life_ymax; ++y) { for (size_t x = 0; x < life_xmax; ++x) { cout << (oldmatrix[y][x] ? 'X' : '.'); } cout << "\n"; } cout << generation << ": Press c to continue, q to quit, and RETURN.\n"; char buffer[256]; //uninitialized variable cin >> buffer; if (strcmp(buffer, "c") != 0) { break; } bool newmatrix[life_ymax][life_xmax]; //uninitialized variable for (size_t y = 0; y < life_ymax; ++y) { const size_t ymin = y <= 0 ? 0 : y - 1; const size_t ymax = y >= life_ymax - 1 ? life_ymax - 1 : y + 1; for (size_t x = 0; x < life_xmax; ++x) { const size_t xmin = x <= 0 ? 0 : x - 1; const size_t xmax = x >= life_xmax - 1 ? life_xmax - 1 : x + 1; //Count how many of the 8 neighbors of element //x, y are turned on. int count = 0; for (size_t y1 = ymin; y1 <= ymax; ++y1) { for (size_t x1 = xmin; x1 <= xmax; ++x1) { //Don't count element x, y itself. if ((x1 != x || y1 != y) && oldmatrix[y1][x1]) { ++count; } } } // Law of Survival if (count == 2) { newmatrix[y][x] = oldmatrix[y][x]; } //Law of Birth else if (count == 3) { newmatrix[y][x] = true; } //Law of Death else { newmatrix[y][x] = false; } } } // Copy newmatrix into oldmatrix. for (size_t y = 0; y < life_ymax; ++y) { for (size_t x = 0; x < life_xmax; ++x) { oldmatrix[y][x] = newmatrix[y][x]; } } } return EXIT_SUCCESS; }