2d array element change

I have 2d array (10x12), and they have 0 and 1s in them. I want to count the 1s in each array and I have two cases here: (1) if an array has more than three 1s, say k many, then generate k - 3 random variables and replace that many 1s with 0s if that random variable corresponds to a 1. (2)if an array has less than three 1s, say q many, then generate 3 - q random variables and replace that many 0s with 1s if that random variable corresponds to a 0. I have the following code so far. I need some suggestions here, I could not construct the code well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int y[10];
int r[10];

for(int i=0; i < 10; i++){          
        for(int j=0; j < 12; j++)
        {
            y[i] += Array[i][j]; 
        }
        if(y[i] > 3)
        {
            f = y[i] - 3;
            if(Array[i][r[f]] ==1)
            Array[i][r[f]] = 0;
        }
        if(y[i] < 3)
        {
            f = 3 - y[i];
            if(Array[i][r[f]] ==0)
            Array[i][r[f]] = 1;
        }
    }
    


Thanks
Last edited on
Topic archived. No new replies allowed.