as I understand your problem is that you can't realy access the arrays from the function?
You can solve this by making the arrays global by declaring them outside of the main function. Get my drift?
as I understand your problem is that you can't realy access the arrays from the function?
You can solve this by making the arrays global by declaring them outside of the main function. Get my drift?
try this:
Code:#include <stdio.h> #include <iostream> #include <fstream> #include <string> void genLife(); void showLife(); int x; int y; int max; char choice; int cell[20][20]; int main(int argc, char *argv[]) { max = 20; for(int row=0;row < max;row++){ for(int colom=0;colom < max;colom++){ cell[row][colom] = 0; } } cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl; do { cout << "Enter coordiantes:\n"; cin >> x >> y; if(x > max || y > max){ cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n"; cin >> x >> y; }else{ cell[x][y] = 1; } } while(x >= 0 && y >= 0); showLife(); return 0; } void showLife(){ int colom = 0; int row = 0; do { cout << "[" << cell[row][colom] << "]"; colom = colom + 1; if (colom == max){ row = row + 1; colom = 0; cout << endl; } } while(row < max && colom < max); cout << "\n\nGo to next generation? (Y/N)" << endl; cin >> choice; if(choice == 'y' || choice == 'Y'){ genLife(); }else{ int life = 0; for(int colom=0;colom < max;colom++){ if(cell[row][colom] == 1){ life = life +1; } } cout << "Thank you for using my world simulator" << endl; } } void genLife(){ int colom = 0; int row = 0; int c = 0; do { if(cell[row][colom + 1] == 1){ c = c +1; } if(cell[row][colom - 1] == 1){ c = c + 1; } if(cell[row +1][colom] == 1){ c = c + 1; } if(cell[row - 1][colom] == 1){ c = c + 1; } if(cell[row - 1][colom +1] == 1){ c = c + 1; } if(cell[row - 1][colom -1] == 1){ c = c + 1; } if(cell[row + 1][colom -1] == 1){ c = c + 1; } if(cell[row + 1][colom +1] == 1){ c = c + 1; } if (c == 3 || c == 2 && cell[row][colom] == 1){ cell[row][colom] = 1; }else{ cell[row][colom] = 0; } if (c == 3 && cell[row][colom] == 0){ cell[row][colom] = 1; } c = 0; colom = colom + 1; if (colom == max){ row = row + 1; colom = 0; } } while(row < max && colom < max); showLife(); }
try this:
Code:#include <stdio.h> #include <iostream> #include <fstream> #include <string> void genLife(); void showLife(); int x; int y; int max; char choice; int cell[20][20]; int main(int argc, char *argv[]) { max = 20; for(int row=0;row < max;row++){ for(int colom=0;colom < max;colom++){ cell[row][colom] = 0; } } cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl; do { cout << "Enter coordiantes:\n"; cin >> x >> y; if(x > max || y > max){ cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n"; cin >> x >> y; }else{ cell[x][y] = 1; } } while(x >= 0 && y >= 0); showLife(); return 0; } void showLife(){ int colom = 0; int row = 0; do { cout << "[" << cell[row][colom] << "]"; colom = colom + 1; if (colom == max){ row = row + 1; colom = 0; cout << endl; } } while(row < max && colom < max); cout << "\n\nGo to next generation? (Y/N)" << endl; cin >> choice; if(choice == 'y' || choice == 'Y'){ genLife(); }else{ int life = 0; for(int colom=0;colom < max;colom++){ if(cell[row][colom] == 1){ life = life +1; } } cout << "Thank you for using my world simulator" << endl; } } void genLife(){ int colom = 0; int row = 0; int c = 0; do { if(cell[row][colom + 1] == 1){ c = c +1; } if(cell[row][colom - 1] == 1){ c = c + 1; } if(cell[row +1][colom] == 1){ c = c + 1; } if(cell[row - 1][colom] == 1){ c = c + 1; } if(cell[row - 1][colom +1] == 1){ c = c + 1; } if(cell[row - 1][colom -1] == 1){ c = c + 1; } if(cell[row + 1][colom -1] == 1){ c = c + 1; } if(cell[row + 1][colom +1] == 1){ c = c + 1; } if (c == 3 || c == 2 && cell[row][colom] == 1){ cell[row][colom] = 1; }else{ cell[row][colom] = 0; } if (c == 3 && cell[row][colom] == 0){ cell[row][colom] = 1; } c = 0; colom = colom + 1; if (colom == max){ row = row + 1; colom = 0; } } while(row < max && colom < max); showLife(); }