c - Scroll through 1d array and change specific values -
my question following. want scroll through 1-d array, check values 4th,5th,8th,9th aka: 0+4*n , 3+4*n. check if value of field 0 , if yes make 1 , stop. if not 0, go next value (0+4*n or 3+4*n) , make 1 , stop. , on.. have done far following. problem updates many values @ once..
{ (i=0; i<nr; i++) { (n=0; n<((nr)/4); n++) if (i==(0+(n*4))) { if (array[i]==0) { array[i]=1; break; } } else if ((i==(3+(n*4)))) { if (array[i]==0) { array[i]=1; break; } } } }
what doing wrong, , doesn't stop updates values @ once?
your interpretation incorrect: 4th, 5th, 8th, 9th etc. correspond 4*n
, 4*n+1
, not 4*n+3
here modified , simplified code:
(i = 0; < nr; i++) { if (i % 4 <= 1) { if (array[i] == 0) { array[i] = 1; break; } } }
Comments
Post a Comment