How to break loop when values in array dont fill it whole?

234 views Asked by At

I have got an array counter[7] and it's always fulfilled with random bits (so 1 and 0 bits/values). Also I have got an other array error_sum[7] where I want to move only the wrong values (previously searched by another algorithm) from counter[7]. The problem is, I don't know how to fill error_sum[7] using a for loop until these wrong values are over.

Example (pseudo code):

counter[7] = {1,0,0,1,1,0,1};

Let's say the wrong bits are {1,0, , ,1,0,1}, so only 5 (wrong) bits I want to move to the error_sum[7] array, to have them there like this:

error_sum[7] = {1,0,1,0,1,NULL,NULL}

so only the wrong bits from counter[7] are moved to error_sum[7].

I know how to move just wrong bits, but I don't know how to do it until there are no more wrong bits. I am doing it with one for loop with if (choosing only wrong bits), so I've got the wrong bits mixed with only zeros or only one-s (bits) don't know why.

////sumowanie par, które są błędne////
        int [] error_sum = new int [7];
        System.out.println("\n"); 
for (int i=0; i<7; i++)
        {
            if (error[i]=="błąd")

            {
                System.out.println(counter[i]+" "+error[i]);
                for (int j=0; j<7; j++)
                {
                    error_sum[j]=counter[i];
                    if (_WHAT CODE INSERT HERE TO BREAK THE LOOP WHEN THERE ARE NO MORE WRONG BITS IN COUNTER[7]??_) break;
                }

            }

Whole my code:

package teleinformatykalab2;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;


public class TeleinformatykaLab2 {

    //Funkcja wypełniająca tablicę ciągiem losowych bitów 0 lub 1 //

    static boolean[] bity;

    public static void Losowanie(int tab[]){

        ////int [] tab = new int[110];

        for (int i = 0; i<110; i++)
        {
            Random r = new Random();
            tab[i] = r.nextInt(2);
            if (i%25==0)
            {
                System.out.println("");
            }
            System.out.print(tab[i]+"("+i+")"+", ");

        }
        System.out.println("");

}
    /////////////////


    //Funkcja wyświetla bity, których indeks w tablicy jest wartością kolejnej potęgi liczby dwa//
    public static void zamienBity(int tab[], int tabBit[])
    {
        ////System.out.println("\n"+tab[0]+tab[109]+"\n");
        //int [] power = new int [7];
        bity = new boolean[7];
        for (int i=0; i<110; i++)
        {

            if (isPowerOfTwo(i))
                {
                 int j = 0;
                 bity[j] = isPowerOfTwo(i);
                 j++;
                 System.out.print(tab[i]+"("+i+")"+", ");

                }
            if (i%50==0)
            {
                System.out.println("");
            }

        }

        System.out.println("\n");

    }

    //Sprawdzenie ciągu kodem Hamminga//
    public static void sprawdzHamming(int tab[], int tabBit[])
    {
        // przypisanie do tabBit bitow o indeksie potęgi dwa
        int [] power = new int [7];
        for(int i=0;i<tabBit.length;i++)
        {
            tabBit[i] = tab[(int)Math.pow(2,i)];
            System.out.print(tabBit[i]+", ");
        }
        System.out.println("////\n");

        //sprawdzenie pierwszej pary bitów
        System.out.println("\nPierwsza para");

        int [] skip = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,36,39,41,43,45,47,49,
                        51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,
                        101,103,105,107,109};    
        for (int i : skip)
        {
             System.out.print(tab[i] + "("+i+")"+",");

                if (i%25==0)
                {
                    System.out.println("");
                }
        }
        System.out.println("");

        //sprawdzenie drugiej pary bitów
        System.out.println("\nDruga para");

         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("");
                }
          }
        System.out.println();

        //sprawdzenie trzeciej pary bitów
        System.out.println("\nTrzecia para");

         int skip3 []= {5,6,7, 12,13,14,15, 20,21,22,23, 28,29,30,31, 36,37,38,39, 44,45,46,47,
                        52,53,54,55, 60,61,62,63, 68,69,70,71, 76,77,78,79, 84,85,86,87, 92,93,94,95, 100,101,102,103, 108,109};
          for (int k : skip3)
          {
          System.out.print(tab[k] + "("+k+")"+",");
          if (k%18==0)
                {
                    System.out.println("");
                }
          }
        System.out.println();

         //sprawdzenie czwartej pary bitów
        System.out.println("\nCzwarta para");

         int skip4 []= {9,10,11,12,13,14,15,16, 25,26,27,28,29,30,31,32, 41,42,43,44,45,46,47,48,
                       57,58,59,60,61,62,63,64, 73,74,75,76,77,78,79,80, 89,90,91,92,93,94,95,96,
                       105,106,107,108,109};
          for (int l : skip4)
          {
          System.out.print(tab[l] + "("+l+")"+",");
          if (l%8==0)
                {
                    System.out.println("");
                }
          }
        System.out.println();

        //sprawdzenie piątej pary bitów
        System.out.println("\nPiąta para");

        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("");
                }
        }
        System.out.println();

        //sprawdzenie szóśtej pary bitów
        System.out.println("\nSzóśta para");

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

        //sprawdzenie siódmej pary bitów
        System.out.println("\nSiódma para");

        int [] sixtyfour = new int [120];

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


    ////Tablica liczników par bitów//
        int [] counter = new int [7];

    ////Tablica wyświetlanych błędów//
        String [] error = new String [7];
  //////////Liczniki bitów jedynkowych////        
        int licznik1=0;
        for (int a=1; a<110;a++)
        {
            if (tab[a]==1)
                licznik1++;
        }
        int l1;
        String spr;
        if (licznik1%2==1) l1=1; else l1=0;
        if (l1==tabBit[0]) error [0] = "ok";//spr="ok";
        else error [0]="błąd";
        counter [0] = l1;

        System.out.println("W grupie 1: "+licznik1+"("+tabBit[0]+")" + " >"+counter[0]+" "+error[0]);



        int licznik2=0;
        for (int b=3; b<108;b++)
        {
            if (tab[b]==1)
                licznik2++;
        }
        int l2;
        if (licznik2%2==1) l2=1; else l2=0;
        if (l2==tabBit[1]) error [1]="ok";
        else error [1]="błąd";
        counter [1] = l2;
        System.out.println("W grupie 2: "+licznik2 +"("+tabBit[1]+")"+" >"+counter [1]+" "+error[1]);


        int licznik3=0;
        for (int c=5; c<109;c++)
        {
            if (tab[c]==1)
                licznik3++;
        }
        int l3;

        if (licznik3%2==1) l3=1; else l3=0;
        if (l3==tabBit[2]) error[2]="ok";
        else error[2]="błąd";
        counter [2] = l3;
        System.out.println("W grupie 3: "+licznik3 +"("+tabBit[2]+")"+ " >"+counter[2]+" "+error[2]);


        int licznik4=0;
        for (int d=9; d<109;d++)
        {
            if (tab[d]==1)
                licznik4++;
        }
        int l4;

        if (licznik4%2==1) l4=1; else l4=0;
        if (l4==tabBit[3]) error[3]="ok";
        else error[3]="błąd";
        counter[3]=l4;
        System.out.println("W grupie 4: "+licznik4 +"("+tabBit[3]+")"+ " >"+counter[3]+" "+error[3]);
        //if (l4==tabBit[3]) System.out.print(" ok");

        int licznik5=0;
        for (int e=17; e<110;e++)
        {
            if (tab[e]==1)
                licznik5++;
        }
        int l5;

        if (licznik5%2==1) l5=1; else l5=0;
        if (l5==tabBit[4]) error[4]="ok";
        else error[4]="błąd";
        counter[4] = l5;
        System.out.println("W grupie 5: "+licznik5 + "("+tabBit[4]+")"+" >"+counter[4]+" "+error[4]);


        int licznik6=0;
        for (int f=33; f<110;f++)
        {
            if (tab[f]==1)
                licznik6++;
        }
        int l6;

        if (licznik6%2==1) l6=1; else l6=0;
        if (l6==tabBit[5]) error[5]="ok";
        else error[5]="błąd";
        counter [5] = l6;
        System.out.println("W grupie 6: "+licznik6+"("+tabBit[5]+")"+" >"+counter[5]+" "+error[5]);


        int licznik7=0;
        for (int g=65;g<110; g++)
        {
            if (tab[g]==1)
                licznik7++;
        }
        int l7;

        if (licznik7%2==1) l7=1; else l7=0;
        if (l7==tabBit[6]) error[6]="ok";
        else error[6]="błąd";
        counter [6] = l7;
        System.out.println("W grupie 7: "+licznik7+"("+tabBit[6]+")"+" >"+counter [6]+" "+error[6]);

        ////sumowanie par, które są błędne////
        int [] error_sum = new int [7];
        System.out.println("\n"); 

        for (int i=0; i<7; i++)
        {
            if (error[i]=="błąd")

            {
                System.out.println(counter[i]+" "+error[i]);
                for (int j=0; j<7; j++)
                {
                    error_sum[j]=counter[i];
                    if (error[i]>) break;
                }

            }
                //System.out.print("//"+power[j]+", ");
                //System.out.println("Bity zacne: " + bity[i]);
        }
        System.out.println("\n");
        for (int i=0; i<7; i++)
        {
            System.out.println(error_sum[i]+", ");
        }



        /*for (int s=0; s<7; s++)
        {
            if (l1==1) break;
        }*/




 /*int arryNum[] = { 2, 3, 4, 5, 4, 4, 3 };
int[] counter = new int[] { 0, 0, 0, 0, 0 };
for (int i = 0; i < arryNum.length; i++) {
    counter[arryNum[i] - 1]++;
}

for (int i = 0; i < counter.length; i++)
    System.out.println((i + 1) + ":" + counter[i]);*/
        /*for (int n=65; n<110; n++)    
        {    
            sixtyfour[n] = tab[n];
            System.out.print(sixtyfour[n]+". ");
            if (n%16==0)
                {
                    System.out.println("");
                }
            for (int a=65; a<110; a++)
            {
                if (a==1)
                {
                    sum=a++;
                }

                    System.out.println(a+sum);
            }
        }*/
        System.out.println();
}


    private static boolean isPowerOfTwo(int x)
    {
        //return (x & (x-1)) ==0;                
        return (x!=0) && ((x&(x-1)) ==0);
    }
    /*public static void powerOfTwo()
    {
        isPowerOfTwo(x);
        System.out.println(x);

    }*/

    public static void main(String[] args) {

        int [] tab = new int[110];


    // wywołanie metody wypełniającej tablicę ciągiem losowych bitów 0 lub 1
        Losowanie(tab);
        System.out.println("\n Bity potęgi dwa: ");
        int [] tabBit = new int [7];


        zamienBity(tab,tabBit);

        System.out.println("////Bity potęgi 2");
        sprawdzHamming(tab,tabBit);
        //powerOfTwo();

        /*int liczby [] = new int[120];
        System.out.println("\n");
        for (int z=1; z<120;z++)
        {
            liczby[z]=z;
            System.out.print(liczby[z]+", ");
            if (z%32==0)
                {
                    System.out.println("");
                }
        }*/

    }
}
1

There are 1 answers

2
ScriptyChris On

This part of code shows me error_sum[7] array, so only wrong bits:

////sumowanie par, które są błędne////
        int [] error_sum = new int [7];
        System.out.println("\n"); 

        for (int i=0; i<7; i++)
        {
            if (error[i]=="błąd")

            {
                System.out.println(counter[i]+" "+error[i]);

                int j=0;
                j++;

                    error_sum[j]=counter[i];

                    System.out.println(error_sum[j]+", ");
            }                
        }

So, after this loop error_sum[7] should be filled (and it appears it is) with only wrong bits. However, if i want to output this array outside this loop, then i got 6 zeros, and on 2nd position, the last wrong bit from error_sum[7].

for (int i=0; i<7; i++)
        {
            System.out.print(error_sum[i]+", '");
        }

This do...while loop however, shows me only last wrong bit from error_sum[7] array instead of all (i dont know why?). So do...while loop shows me same wrong (last from array) bit as above for-loop.

do
                    {
                        int u=0;
                        u++;
                        System.out.print(error_sum[u]+", ,");
                    }
                    while(error_sum==null);

Only in first for-loop when i move origin bits between arrays, i got proper output.