Problem with temporary array

Reply

Join Date: May 2009
Posts: 19
Reputation: wewehalim is an unknown quantity at this point 
Solved Threads: 0
wewehalim wewehalim is offline Offline
Newbie Poster

Problem with temporary array

 
0
  #1
Sep 15th, 2009
I have a program that use 2 arrays, 1 is the cells array and the other is temporary array

this program generate 20 rows and 64 columns(cells)
each cell compare the itself with the cell on it left and right.

The problem is, the program only work from row 1 to row 2.
row 3 until 20 did not work.
the cell changes it state correctly from row 1 to 2 only.
i think it is because of the temp array.
can someone check it?

the problem is in method operateCell()

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. /*******************************************************************\
  7.   Program that generates a simple 1 dimendional automata.
  8.   The automata is described by a line of cells that
  9. can have 4 possible states; space,".", "+", "#".
  10.   Each cell examines the cell on either side of it.
  11.   The following rules are then applied to each cell:
  12. 1. If either one of the neighbouring cells has a state equal to
  13.   the next state then the cell is changed to the next stage.
  14. 2. If both neighours have the same state as the cell then the
  15.   cell is changed to the next state.
  16. 3. If neither rurle 1 or 2 applies then the cell remains unchanged.
  17. \*******************************************************************/
  18.  
  19. namespace cell_autom
  20. {
  21. class Automata
  22. {
  23. private const int CELLSIZE = 64;
  24. private const int GENERATION = 20;
  25. private Cell[] Cells;
  26. private Cell[] tempCells;
  27.  
  28. public Automata()
  29. {
  30. Cells = new Cell[CELLSIZE];
  31. tempCells = new Cell[CELLSIZE];
  32. for (int i = 0; i < CELLSIZE; i++)
  33. {
  34. Cells[i] = new Cell();
  35. tempCells[i] = new Cell();
  36. }
  37. }
  38.  
  39. /// <summary>
  40. /// Generates random number and assign the symbol to each number
  41. /// </summary>
  42. public void GenerateCell(int seed)
  43. {
  44. int rand;
  45.  
  46. // set up random object and initialise with seed
  47. Random r = new Random(seed);
  48. for (int i = 0; i < CELLSIZE; i++)
  49. {
  50. rand = r.Next(4);
  51.  
  52. // applying symbol to generated number
  53. if (rand == 0)
  54. {
  55. Cells[i].DefaultCell = CellState.SPACE;
  56. tempCells[i].DefaultCell = CellState.SPACE;
  57. }
  58. else if (rand == 1)
  59. {
  60. Cells[i].DefaultCell = CellState.DOT;
  61. tempCells[i].DefaultCell = CellState.DOT;
  62. }
  63. else if (rand == 2)
  64. {
  65. Cells[i].DefaultCell = CellState.PLUS;
  66. tempCells[i].DefaultCell = CellState.PLUS;
  67. }
  68. else
  69. {
  70. Cells[i].DefaultCell = CellState.HASH;
  71. tempCells[i].DefaultCell = CellState.HASH;
  72. }
  73. }
  74. Console.Write("Gen 1 ");
  75. }
  76.  
  77. /// <summary>
  78. /// Print out the cells using symbol
  79. /// </summary>
  80. public void PrintCell()
  81. {
  82. // print the cells in the row
  83. for (int i = 0; i < CELLSIZE; i++)
  84. {
  85. if (Cells[i].DefaultCell == CellState.SPACE)
  86. Console.Write(" ");
  87. else if (Cells[i].DefaultCell == CellState.DOT)
  88. Console.Write(".");
  89. else if (Cells[i].DefaultCell == CellState.PLUS)
  90. Console.Write("+");
  91. else
  92. Console.Write("#");
  93. }
  94. Console.WriteLine();
  95. }
  96.  
  97. /// <summary>
  98. /// Create a copy array
  99. /// </summary>
  100. public void CopyArray()
  101. {
  102. for (int i = 0; i < CELLSIZE; i++)
  103. {
  104. tempCells[i] = Cells[i];
  105. }
  106. }
  107.  
  108. public void OperateCell()
  109. {
  110. for (int j = 0; j < GENERATION - 1; j++)
  111. {
  112. Console.Write("Gen {0,3} ", j + 2);
  113. for (int i = 0; i < CELLSIZE; i++)
  114. {
  115. if (i == 0)
  116. {
  117. if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[63].DefaultCell)
  118. Cells[i].ChangeState();
  119. if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[63].DefaultCell)
  120. Cells[i].ChangeState();
  121. }
  122. else if (i == 63)
  123. {
  124. if (Cell.NextCellState(tempCells[i]) == tempCells[0].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell)
  125. Cells[i].ChangeState();
  126. if (tempCells[i].DefaultCell == tempCells[0].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell)
  127. Cells[i].ChangeState();
  128. }
  129. else if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell)
  130. {
  131. Cells[i].ChangeState();
  132. }
  133. else
  134. {
  135. if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell)
  136. Cells[i].ChangeState();
  137. }
  138. }
  139. CopyArray();
  140. PrintCell();
  141. }
  142. }
  143. }
  144. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Problem with temporary array

 
0
  #2
Sep 15th, 2009
STEP 1: Place several comments in your OperateCell method (aka operation1 method). Place the comments above:
* every loop
* every logical comparison
* every method called within the nested loop

When doing this, provide a good explanation of what the statement's purpose is--what is it supposed to do exactly.

By doing this, you will:
* have a better chance of discovering what is wrong yourself
* give others a much better chance of helping you to figure it out
* help to understand the program later on when it may not still be fresh in yours and/or someone else's mind

STEP 2 (can be done in conjunction with STEP 1): Refer to the rules/logic of program stated at the beginning your program:
* Compare it to the descriptive comments you just made in OperateCell
* update these rules to reflect any detail not already defined.
* update comments in OperateCell too if a detail already defined in the rules is not explained in the statement comments in OperateCell

Cheers!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 235 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC