1 / 26

Industrial Age

Industrial Age . After the Civil War the U.S. was still and agricultural nation. 60 years later the U.S. becomes the leading industrial power in the world. The industrial boom was due to several factors. . A wealth of natural resources Government support for business

cameo
Télécharger la présentation

Industrial Age

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresCSCI 132, Spring 2014Lecture 4Implementing Life

  2. Rules of Life • The neighbors of a given cell are the eight cells that touch it. • Every cell is either living or dead. • A living cell stays alive in the next generation if it has either 2 or 3 living neighbors; otherwise it dies (of loneliness or overcrowding) • A dead cell becomes alive in the next generation if it has exactly 3 neighboring cells that are already alive. • All births and deaths take place at exactly the same time for each generation.

  3. Specifications What do we want the program to do?

  4. Specifications We want the program to: • List instructions • Allow user to initialize grid • Print out grid at each generation • Update next generation • Repeat until user says stop

  5. Design • Class (noun) • The Life game itself • Methods or functions • (What methods or functions should we have?) • Print instructions • Initialize • Print • Update

  6. The Client Code #include "utility.h" #include "life.h" int main( ) { Life configuration; instructions( ); configuration.initialize( ); configuration.print( ); cout << " Continue viewing new generations? "<<endl; while (user_says_yes( )) { configuration.update( ); configuration.print( ); cout << " Continue viewing new generations? "<<endl; } }

  7. Testing with Stubs void instructions( ) { } bool user_says_yes( ) { return(true); } class Life { // In file life.h public: void initialize( ); void print( ); void update( ); }; void Life ::initialize( ) { } // In file life.cc void Life ::print( ) { } void Life ::update( ) { }

  8. Class Specification const int maxrow = 20, maxcol = 60;// grid dimensions class Life { public: void initialize( ); void print( ); void update( ); private: int grid[maxrow +2][maxcol +2]; // allows for two extra rows and columns int neighbor_count(int row, int col); };

  9. Class implementation int Life ::neighbor_count(int row, int col) { int i, j; int count = 0; //we will fill this in return count; }

  10. Class implementation int Life ::neighbor_count(int row, int col) { int i, j; int count = 0; for (i = row - 1; i <= row +1; i ++) { for (j = col - 1; j <= col +1; j ++) { count += grid[i][j]; //Increase the count if neighbor is alive. } } count -=grid[row][col]; // Reduce count, since cell is not its own neighbor. return count; }

  11. Using a Driver int main ( ) // driver for neighbor_count( ) { Life configuration; configuration.initialize( ); //we will fill this in. }

  12. Using a Driver int main ( ) // driver for neighbor_count( ) { Life configuration; configuration.initialize( ); for (row = 1; row <= maxrow; row ++){ for (col = 1; col <= maxrow; col ++) { cout << configuration.neighbor_count(row, col) << " " ; } cout << endl; } }

  13. int Life ::update( ) { int row, col; int new_grid[maxrow+2][maxcol+2]; //we will fill this in for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxcol; col++) { grid[row][col] = new_grid[row][col]; } //end inner for loop } //end outer for loop } //end update()

  14. int Life ::update( ) { int row, col; int new_grid[maxrow+2][maxcol+2]; for (row = 1; row <= maxrow; row ++) { for (col = 1; col <= maxcol; col ++) { switch (neighbor_count(row, col)) { case 2: new_grid[row][col] = grid[row][col]; break; case 3: new_grid[row][col] = 1; break; default: new_grid[row][col] = 0; } //end switch } //end inner for loop } //end outer for loop for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxcol; col++) { grid[row][col] = new_grid[row][col]; } //end inner for loop } //end outer for loop } //end update()

  15. int Life ::initialize( ) { int row, col; for (row = 0; row <= maxrow+1; row++) { for (col = 0; col <= maxcol+1; col++) { grid[row][col] = 0; } //end inner for loop } //end outer for loop cout << "List the coordinates for the living cells." << endl; cout << "Terminate the list with the pair -1 -1 " << endl; cin >> row >> col; // we will fill this in } //end initialize()

  16. int Life ::initialize( ) { int row, col; for (row = 0; row <= maxrow+1; row++) { for (col = 0; col <= maxcol+1; col++) { grid[row][col] = 0; } //end inner for loop } //end outer for loop cout << "List the coordinates for the living cells." << endl; cout << "Terminate the list with the pair -1 -1 " << endl; cin >> row >> col; while (row != -1 || col != -1) { if (row >= 1 && row <= maxrow) { if ( col >= 1 && col <= maxcol ) { grid[row][col] = 1; } else { cout << "Column " << col << " is out of range." << endl; } // end inner if-else } else { cout << "Row " << row << " is out of range." << endl; } //end outer if - else cin >> row >> col; } //end while loop } //end initialize()

  17. Order of Development 1. initialize( ) 2. print( ) 3. neighbor_count ( ) 4. update( )

More Related