Write a program that:
Prompts the user to enter the number of cells C.
Declares an integer array cell[] with C elements.
Prompts the user to enter the number of time steps N.
Prompts the user to enter the list of indices of cells that contain 1
(enter negative index to nish).
Print a “ruler” line with C digits, 0 through 9, repeated
(see sample output below for clari cation).
Run the cellular automaton for N time steps, using the rules de ned above. On each time step, display the cells on one line,
printing a ‘#’ if the cell contains a 1, or a space if the cell contains a 0.
Your program\'s output should match the sample runs
public static void main(String[] args){
}
The data[] array contains data.length cells, with each cell containing either 1 or 0. displayCells() prints each cell of the data[] array on the same line, displaying a ‘#’ if the cell contains a 1, a space if the cell contains a 0.
The data[] array contains data.length cells, either 1 or 0. updateCells() updates each cell in the data[] array, according to the rules de ned earlier in this handout, for one step. (Hint: you will need to create a temporary array in updateCells(), make the updates into the temporary array, and copy the temporary array to the data[] array before the method returns.)
#include
002 #include 003 using namespace std; 004 005 void setupRow(); 006 void printRow(); 007 void updateRow(); 008 int findNextCellState(int left_neighbor, int cell, int right_neighbor); 009 010 const int WIDTH = 80; 011 int row[WIDTH]; 012 int i = 0; 013 int temp[WIDTH]; 014 015 int main() 016 { 017 setupRow(); 018 printRow(); 019 020 for (int i = 0; i < 100; i++) 021 { 022 updateRow(); 023 printRow(); 024 } 025 026 return 0; 027 } 028 029 // The setupRow function creates an initial state for the cellular autonoma 030 void setupRow() 031 { 032 for(i = 0; i < 80; ++i) 033 { 034 if(i != 39) 035 row[i] = 0; 036 037 if(i == 39) 038 row[i] = 1; 039 } 040 } 041 042 // The printRow function outputs the state of the row to the user. For all 043 // cells that are 0, it outputs a space (\" \"), for all cells that are 1, it 044 // outputs a star (\"*\") 045 void printRow() 046 { 047 for(i = 0; i < 80; ++i) 048 { 049 if(row[i] == 0) 050 { 051 cout << \" \"; 052 } 053 054 if(row[i] == 1) 055 { 056 cout << \"*\"; 057 } 058 } 059 } 060 061 // The updateRow function updates the contents of the row after calculating 062 // the next states using the findNextCellState function 063 void updateRow() 064 { 065 temp[WIDTH] = findNextCellState(0, row[0], row[1]); 066 067 for(i = 0; i < 80; ++i) 068 { 069 temp[i] = findNextCellState(row[i-1], row[0], row[i+1]); 070 row[i] = temp[i]; 071 } 072 073 } 074 075 // The states for this are: 076 // 077 // 111 110 101 100 011 010 001 000 078 // 0 0 0 1 1 1 1 0 079 080 int findNextCellState(int left_neighbor, int cell, int right_neighbor) 081 { 082 if (0 == cell) 083 { 084 if (1 == (left_neighbor + right_neighbor)) 085 { 086 return 1; 087 } 088 else 089 { 090 return 0; 091 } 092 } 093 else 094 { 095 if (1 == left_neighbor + right_neighbor) 096 { 097 return 0; 098 } 099 else 100 { 101 return 1; 102 } 103 } 104 }