Previous index in an array c# -
i have array initialized such:
int[] myarray = new int[] {9, 8, 7, 3, 4, 5, 6, 2, 1}; i have for() loop searching array highest value each time using:
int maxvalue = myarray.max(); int maxindex = myarray.tolist().indexof(maxvalue); it keeps finding 9 highest value.
i want first set indexed value randomized value below current maxvalue above -1 , continue searching array next maxvalue , print console.
(if values reach value == 0 simulation stops) <- part know how do.
is possible? if so, how?
i guess might want. let me know how works you.
using system; using system.linq; public class program { private static random random = new random(); public static void main() { int[] myarray = new int[] {9, 8, 7, 3, 4, 5, 6, 2, 1}; simulate(myarray); } static void simulate(int[] myarray) { int maxvalue = myarray.max(); console.writeline(string.join(" ",myarray)); var continuesimulation = true; do{ int maxindex = myarray.tolist().indexof(maxvalue); var randomvalue = random.next(0, maxvalue); myarray[maxindex] = randomvalue; maxvalue = myarray.max(); if (maxvalue == 0) continuesimulation = false; console.writeline(string.join(" ",myarray)); }while(continuesimulation); } } you can check out on this fiddle.
hope helps!
Comments
Post a Comment