| | |
Problem with temporary array
![]() |
•
•
Join Date: May 2009
Posts: 19
Reputation:
Solved Threads: 0
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()
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()
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Linq; using System.Text; /*******************************************************************\ Program that generates a simple 1 dimendional automata. The automata is described by a line of cells that can have 4 possible states; space,".", "+", "#". Each cell examines the cell on either side of it. The following rules are then applied to each cell: 1. If either one of the neighbouring cells has a state equal to the next state then the cell is changed to the next stage. 2. If both neighours have the same state as the cell then the cell is changed to the next state. 3. If neither rurle 1 or 2 applies then the cell remains unchanged. \*******************************************************************/ namespace cell_autom { class Automata { private const int CELLSIZE = 64; private const int GENERATION = 20; private Cell[] Cells; private Cell[] tempCells; public Automata() { Cells = new Cell[CELLSIZE]; tempCells = new Cell[CELLSIZE]; for (int i = 0; i < CELLSIZE; i++) { Cells[i] = new Cell(); tempCells[i] = new Cell(); } } /// <summary> /// Generates random number and assign the symbol to each number /// </summary> public void GenerateCell(int seed) { int rand; // set up random object and initialise with seed Random r = new Random(seed); for (int i = 0; i < CELLSIZE; i++) { rand = r.Next(4); // applying symbol to generated number if (rand == 0) { Cells[i].DefaultCell = CellState.SPACE; tempCells[i].DefaultCell = CellState.SPACE; } else if (rand == 1) { Cells[i].DefaultCell = CellState.DOT; tempCells[i].DefaultCell = CellState.DOT; } else if (rand == 2) { Cells[i].DefaultCell = CellState.PLUS; tempCells[i].DefaultCell = CellState.PLUS; } else { Cells[i].DefaultCell = CellState.HASH; tempCells[i].DefaultCell = CellState.HASH; } } Console.Write("Gen 1 "); } /// <summary> /// Print out the cells using symbol /// </summary> public void PrintCell() { // print the cells in the row for (int i = 0; i < CELLSIZE; i++) { if (Cells[i].DefaultCell == CellState.SPACE) Console.Write(" "); else if (Cells[i].DefaultCell == CellState.DOT) Console.Write("."); else if (Cells[i].DefaultCell == CellState.PLUS) Console.Write("+"); else Console.Write("#"); } Console.WriteLine(); } /// <summary> /// Create a copy array /// </summary> public void CopyArray() { for (int i = 0; i < CELLSIZE; i++) { tempCells[i] = Cells[i]; } } public void OperateCell() { for (int j = 0; j < GENERATION - 1; j++) { Console.Write("Gen {0,3} ", j + 2); for (int i = 0; i < CELLSIZE; i++) { if (i == 0) { if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[63].DefaultCell) Cells[i].ChangeState(); if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[63].DefaultCell) Cells[i].ChangeState(); } else if (i == 63) { if (Cell.NextCellState(tempCells[i]) == tempCells[0].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell) Cells[i].ChangeState(); if (tempCells[i].DefaultCell == tempCells[0].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell) Cells[i].ChangeState(); } else if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell) { Cells[i].ChangeState(); } else { if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell) Cells[i].ChangeState(); } } CopyArray(); PrintCell(); } } } }
•
•
Join Date: Jul 2009
Posts: 977
Reputation:
Solved Threads: 224
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!
* 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!
![]() |
Similar Threads
- Pls help me... in array problem.. (C++)
- Multidimensional array problem (PHP)
- Problem with byte array to string (Java)
- Ordering an array (Java)
- Problem with multiDimensional array? (C)
- Problem with large array (C++)
- Array problem (Java)
Other Threads in the C# Forum
- Previous Thread: synchronise with IMAP in outlook
- Next Thread: Result of the two textboxes print in third automatically.
Views: 235 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net bitmap box broadcast button c# check checkbox class code color connectionproblem control conversion csharp custom data database datagrid datagridview dataset datatable datetimepicker degrees display dll drawing event excel file form format forms ftp function game gcd gdi+ graphics image index input internet label list listbox login math mysql object operator pda picturebox print programming recursion remote remoting resource resourcefile richtextbox round save saving search server socket sounds sql sql-server start statistics stored stream string text textbox threading time timer treeview update url validation vc++ video visual visualstudio webbrowser webcam windows winforms wpf xml





