Tuesday, 15 July 2014

visual studio 2012 - C# Iterating an If statement with new value each time -



visual studio 2012 - C# Iterating an If statement with new value each time -

is possible iterate nested if statements new value every single iteration? trying build 1-dimensional cellular automata (for homework, cannot deny it) , i'm new c# next code no uncertainty assure. have tried create programme using straightforward, basic, diy methods available , have run myself rut.

i've got string of 1's , 0's of length 8, say

string y; y = "11110000";

i want break set in 8 substring sets of 3 each set comprising of value in y single value on either side of it. counting 0, 3rd set 110, 7th 001. substrings provide 1st 6th set can't loop them around y liking defined following-

y1=y.substring(7,1)+y+y.substring(0,1);

using y1 able out substrings necessary. these defined pretty follows-

string a0, a1, a2, a3, a4, a5, a6, a7; a0 = y1.substring(0, 3); a1 = y1.substring(1, 3); a2 = y1.substring(2, 3); a3 = y1.substring(3, 3); a4 = y1.substring(4, 3); a5 = y1.substring(5, 3); a6 = y1.substring(6, 3); a7 = y1.substring(7, 3);

the rules next generation of cellular automata user in program- user can take whether or not substring, 111->0 or 1 iterations. used (an awful lot of) if tables in next way each substring

{ if (a0=="000") { console.write(a); } else if (a0=="001") { console.write(b); } else if (a0 =="010") { console.write(c); } else if (a0 == "011") { console.write(d); } else if (a0 == "100") { console.write(e); } else if (a0 == "101") { console.write(f); } else if (a0 == "110") { console.write(g); } else if (a0 == "111") { console.write(h); } }

where a,b,c,d,e,f,g,h ints , rules chosen user. instance user decides each set 000 should result in 1 value, a=1. b corresponds {0,0,1}, c {0,1,0} , on. obvious problem method end 1 generation in ints can't at. i'd love replace y1 new generation (converted string). if isn't possible allow me know!

this link might clear things bit

and here's how have gotten a+ :d

private static int[,] hippriestshomework() { string y = "11110000"; console.writeline(y); var rules = new[] { new {pattern = 0, result = 0}, new {pattern = 1, result = 1}, new {pattern = 2, result = 1}, new {pattern = 3, result = 1}, new {pattern = 4, result = 1}, new {pattern = 5, result = 0}, new {pattern = 6, result = 0}, new {pattern = 7, result = 0}, }; dictionary<int, int> ruleslookup = new dictionary<int, int>(); foreach(var rule in rules) { ruleslookup.add(rule.pattern, rule.result); } int numgenerations = 10; int inputsize = y.length; int[,] output = new int[numgenerations, inputsize]; int[] items = new int[y.length]; for(int inputindex = 0; inputindex< y.length; inputindex++) { string token = y.substring(inputindex, 1); int item = convert.toint32(token); items[inputindex] = item; } int[] working = new int[items.length]; items.copyto(working, 0); (int generation = 0; generation < numgenerations; generation++) { (uint y_scan = 0; y_scan < items.length; y_scan++) { int = items[(y_scan - 1) % items.length]; int b = items[y_scan % items.length]; int c = items[(y_scan + 1) % items.length]; int pattern = << 2 | b << 1 | c; var match = rules[pattern]; output[generation, y_scan] = match.result; working[y_scan] = match.result; console.write(match.result); } working.copyto(items, 0); console.writeline(); } homecoming output; }

c# visual-studio-2012 if-statement cellular-automata

No comments:

Post a Comment