SlideServe
Menu
Préférences
© SlideServe · v2026

Industrial Age

cameo avatar
26 diapositives · 569 Views · Oct 14, 2014

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

Industrial Age - slide 1
01 / 26Industrial Age
1 / 17

Télécharger la présentation

Industrial Age
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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( )