Thursday 15 August 2013

c# - When i change the value of an multidimensional array, the value at the same position of another array changes too. Why? -



c# - When i change the value of an multidimensional array, the value at the same position of another array changes too. Why? -

it took me much time figure out.

i have 2 multidimensional arrays:

int[,] oldgeneration = new int[widthx + 1, heighty + 1]; int[,] nextgeneration = new int[widthx + 1, heighty + 1];

later in code after set values in "nextgeneration" create arrays contain same.

oldgeneration = nextgeneration;

when check values while running programme working.

a bit below alter 1 value of "nextgeneration , when that, same value in "oldgeneration" changed well.

can tell me why?

using system; namespace cgol { class cgol_base { static void main(string[] args) { //declare , define (initialize) variables used programme int widthx = 5, heighty = 5, iterations = 5, speed = 1000, random = 0, cellsalive = 0; //declare multidimensional arrays actual generation , next generation int[,] oldgeneration = new int[widthx + 1, heighty + 1]; int[,] nextgeneration = new int[widthx + 1, heighty + 1]; //########### initialize "game board" ########################## //set game board contain dead cells (int y = 1; y <= heighty; y++) { (int x = 1; x <= widthx; x++) { nextgeneration[x, y] = 0; } } //set pattern oscillating (moving) construction 3 living cells (can changed @ will) nextgeneration[3, 2] = 1; nextgeneration[3, 3] = 1; nextgeneration[3, 4] = 1; //set oldgeneration equal nextgeneration calculation can work oldgeneration = nextgeneration; //################################################################## //start iterationcounter (int iteration = 1; iteration <= iterations; iteration++) { //########### calculate actual generation ###################### //calculate how game board alter usual cgol rules if (iteration >= 2) //without this, initialization above not work { (int y = 1; y <= heighty; y++) { (int x = 1; x <= widthx; x++) { //########### check surrounding cells ########################## //check how much cells, surrounding actual cell, still live //to calculate later how game board alter cellsalive = 0; (int = -1; <= 1; i++) { (int n = -1; n <= 1; n++) { if (i == 0 && n == 0) { continue; } //check if array index out of array range (for illustration when index smaller 0) if (x + n == 0 || x + n > widthx || y + == 0 || y + > heighty) { continue; } if (oldgeneration[(x + n), (y + i)] == 1) { cellsalive++; } } } //################################################################## //if dead cell got 3 living neighbours, cell become live in next generation if (oldgeneration[x, y] == 0 && cellsalive == 3) { nextgeneration[x, y] = 1; } //if living cell got less 2 living neighbours, cell die in next generation else if (oldgeneration[x, y] == 1 && cellsalive < 2) { nextgeneration[x, y] = 0; //oldgeneration[x, y] changed 0 if not written here????? why? } //if living cell got 2 or 3 living neighbours, cell remain live in next generation else if (oldgeneration[x, y] == 1 && (cellsalive == 2 || cellsalive == 3)) { nextgeneration[x, y] = 1; } //if living cell got more 3 living neighbours, cell die in next generation else if (oldgeneration[x, y] == 1 && cellsalive > 3) { nextgeneration[x, y] = 0; } } } console.readkey(); } //################################################################## //########### draw "game board" ############################ //makes console window empty :) console.clear(); //"for" making new rows console.write("\n"); (int y = 1; y <= heighty; y++) { //"for" writing "o"'s in 1 row (int x = 1; x <= widthx; x++) { if (nextgeneration[x, y] == 1) { console.write("o "); } else { console.write(" "); } } console.write("\n"); } console.writeline("iteration: {0}", iteration); system.threading.thread.sleep(speed); //at end, create actual generation same old generation calculated next oldgeneration = nextgeneration; //################################################################## } console.readkey(); } } }

when state:

oldgeneration = nextgeneration;

you're assigning reference of nextgeneration oldgeneration it's normal alter 1 reflect other.

if want create disjoint copy, should utilize array.copy ( http://msdn.microsoft.com/it-it/library/y5s0whfd(v=vs.110).aspx )

c# arrays multidimensional-array conways-game-of-life

No comments:

Post a Comment