How to group an array of variables based on flags assigned to them with minimal code in C# -
i have array has 4 elements example :
int[] = new int[5];
the value : a[0] = 10,a[1]=5,a[2]=15,a[3]=10,a[4]=0;
i have flag ,
public bool[] flag = new bool[4]{false,false,false,false};
based on need assign above values variable named b[5]
.
if flag false
,it add
existing values of b ,
else reset values of a
b
.
i have tried following code seems lengthy
count[0]=20,count[1]=20;count[2]=20;count[3]=20; flag[0]=true,flag[1]=true,flag[2]=true,flag[3]=false; void display(int[] count,int[]flag) { if (flag[0] == true) { resetcount[0] = count[0]; } if(flag[1]==true) { resetcount[1] = count[1]; } if (flag[2] == true) { resetcount[2] = count[2]; } if(flag[3]==true) { resetcount[3] = count[3]; } if (flag[0] == false) { resetcount[0] += count[0]; } if (flag[1] == false) { resetcount[1] += count[1]; } if (flag[2] == false) { resetcount[2] += count[2]; } if (flag[3] == false) { resetcount[3] += count[3]; } }
here resetcount[0]=10,resetcount[1]=10,resetcount[2]=10,resetcount[3]=10;
if flag false count value added , if true count value set reset count above 0,1,2
count[0]=20,count[1]=20;count[2]=20;count[3]=20; flag[0]=true,flag[1]=true,flag[2]=true,flag[3]=false; resetcount[0]=10,resetcount[1]=10,resetcount[2]=10,resetcount[3]=10;
so resetcount :
resetcount[0]=20,resetcount[1]=20,resetcount[2]=20,resetcount[3]=30;
like loop?
count[0]=20,count[1]=20;count[2]=20;count[3]=20; flag[0]=true,flag[1]=true,flag[2]=true,flag[3]=false; void display(int[] count,int[]flag) { (int = 0 ; i<=3 ; i++) { if (flag[i]) { resetcount[i] = count[i]; } else { resetcount[i] += count[i]; }
}
Comments
Post a Comment