Guest User

Untitled

a guest
Jan 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // CSCI 1410 Programming assignment 5
  2. // Written by Hanyuan Li <hanyuan.li@ucdenver.edu>
  3. // Elementary cellular automata
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. void setup_row();
  10. void print_row();
  11. void update_row();
  12. int find_next_cell_state(int left_neighbor, int cell, int right_neighbor);
  13. int z;
  14.  
  15. const int WIDTH=80;
  16. int row[WIDTH];
  17.  
  18. int main()
  19. {
  20. setup_row();
  21. print_row();
  22.  
  23. for(int i = 0; i < 42; i++)
  24. {
  25. // update_row();
  26. print_row();
  27. }
  28. }
  29.  
  30. // The setup_row function creates an initial state for the cellular autonoma
  31. void setup_row()
  32. {
  33. for(int z = 0; z < 80; z++)
  34. {
  35. row[z] = 0;
  36. row[39] = 1;
  37. cout << row[z];
  38. }
  39. cout << endl;
  40. }
  41.  
  42. // The print_row function outputs the state of the row to the user. For all
  43. // cells that are 0, it outputs a space (" "), for all cells that are 1, it
  44. // outputs a star ("*")
  45. void print_row()
  46. {
  47. if(row[z] == 0)
  48. {
  49. cout << " ";
  50. }
  51. else
  52. {
  53. cout << "*";
  54. }
  55. }
  56.  
  57. // The update_row function updates the contents of the row after calculating
  58. // the next states using the find_next_cell_state function
  59. void update_row(int left_neighbor, int cell, int right_neighbor)
  60. {
  61. for(int x = 0; x < 80; x++)
  62. {
  63. left_neighbor = row[x];
  64. cell = row[x+1];
  65. right_neighbor = row[x+2];
  66. row[x] = find_next_cell_state(left_neighbor, cell, right_neighbor);
  67. }
  68. }
  69.  
  70. // The find_next_cell_state function implements the Rule 30 celular autonoma rules.
  71. // The states for this are:
  72. //
  73. // 111 110 101 100 011 010 001 000
  74. // 0 0 0 1 1 1 1 0
  75.  
  76. int find_next_cell_state(int left_neighbor, int cell, int right_neighbor)
  77. {
  78. if (0 == cell)
  79. {
  80. if (1 == (left_neighbor + right_neighbor))
  81. {
  82. return 1;
  83. }
  84. else
  85. {
  86. return 0;
  87. }
  88. }
  89. else
  90. {
  91. if (1 == left_neighbor)
  92. {
  93. return 0;
  94. }
  95. else
  96. {
  97. return 1;
  98. }
  99. }
  100. }
Add Comment
Please, Sign In to add comment