How to repetitively output and skip certain values from array by loop?

2.4k views Asked by At

I have array[110] filled with integer (row) of bits (only 1 and 0), I need to divide it to 7 less rows of bits by output and skip repetitively, starting from certain (marked as next power of number 2) index of array[110] to end of this array[110].

Now by 7 different rows (groups,arrays) I mean rows of bits, starting from bits which indexes are next power of number 2, so: 1,2,4,8,16,32... Also in each next row (group), values have to be output and skipped by the number of index, which is next power of number 2 in array. So for example output 4, skip 4... or output 8, skip 8, and so on to last group, where of index in array is next power of 2.
Additionally, first bit of this smaller rows is always skipped or marked as unknown ("?")
I'll treat that indexes in array[110] starts from 1, not 0 like it's natural in arrays - easier description of my problem.

In example (i marked indexes of array in "()"): 1(1),1(2), 0(3), 0(4), 1(5), 1(6), 1(7), 0(8), 1(9), 1(10), 0(11).

In this example 1st group (output 1, skip 1) will be: 1(1) <-unknown(?), 1(2)<-skip, 0(3)<-output, 0(4)<-skip, 1(5)<-output, 1(6)<-skip, 1(7)<-output, 0(8)<-skip, 1(9)<-output, 1(10)<-skip, 0(11)<-output...until end of array[110]

2nd group will be (starting from index 2; output 2 and skip 2; first value is considered unknown): 1(2)<-unknown(?), 0(3)<-output, 0(4)<-skip, 1(5)<-skip, 1(6)<-output, 1(7)<-output, 0(8)<-skip, 1(9)<-skip, 1(10)<-output, 0(11)<-output...untill end of array[110]

3rd group will be (starting from index 4 as next power of two; output 4 and skip 4; first value is unknown): 0(4)<-unknown(?), 1(5)<-output, 1(6)<-output, 1(7)<-output, 0(8)<-skip, 1(9)<-skip, 1(10)<-skip, 0(11)<- skip...until end of array[110]

It's part of Hamming Code. For better understanding: https://www.youtube.com/watch?v=JAMLuxdHH8o

Now, my method is to create 7 different arrays, like group1[], group2[]... and manually insert there certain indexes which I want to be skipped. Then in for-loops I fill each next group[] arrays only with "output" bits.

But I’d like for-loop to do it for me repetitively. Just code there to skip each 2, 4, 8 (and so on) values and output each 2,4,8 (...) values. Thus 7 group[] arrays with different, but static "distances" of output and skip. How to do it?

My code is:

int skip2 []= {3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47, 
                        50,51,54,55,58,59,62,63,66,67,70,71,74,75,78,79,82,83,86,87,90,91,94,95,98,99,102,103,106,107}; 
          for (int j : skip2)
          {
          System.out.print(tab[j] + "("+j+")"+",");
          if (j%25==0)
                {
                    System.out.println("");
                }
          }

And for bigger values from later groups (this is from 5th group, so starting from 16 index - but skipping it as it's unknown):

for (int m=17; m<110; m++)
        {
            if ((m>=32 && m<=47) && (m>=64 && m<=79) && (m>=96 && m<=111)) continue;
            System.out.print(tab[m] + "("+m+")"+",");
            if (m%16==0)
                {
                    System.out.println("");
                }
        }

Second way is more mathematical, because I must count how many indexes skip. However maybe there is method to write how many output and how many skip?

1

There are 1 answers

0
ScriptyChris On

Solved:

        int x=0;
        int sum=0;
        boolean w = true;
        System.out.println("Bity potegi CZWARTEJ");
        for (int i = 0; i < 110; i++)
        {
            if (i<=4) continue;

            x++;
            if (x == 4)
            {
                x = 0;
                w = !w;
            }


            if (i%20==0) System.out.println("");
            if (w) System.out.print(tab[i]+"("+i+")"+",");
            sum++;
            if (i==109) System.out.println("\n"+sum);

        }