I want to fill tab[110] array with random bits (so 1 and 0). Don't know how to fill with values 1-110, but not 0-109. My for-loop:
for (int i = 0; i<tab.length; i++)
{
Random r = new Random();
tab[i] = r.nextInt(2);
if (i%25==0)
{
System.out.println("");
}
System.out.print(tab[i]+"("+i+")"+", ");
}
I know that usually it's simple, just tab[i]=i+1;, but when i put a random number into array it doesn't work for me. I tried to add "+1" everywhere in this loop and only effect i got is when i added it to System.out.println - but then i got fake output that it's 1-110, when in array it's still 0-109.
whole code:
package teleinformatykalab2;
import java.util.Random;
public class TeleinformatykaLab2 {
public static void losujBity(int tab[]) // Funkcja losuje ciag 110 bitow i umieszcza je w utworzonej tabeli
{
System.out.println("Ciag 110 bitow: ");
for (int i = 0; i<tab.length; 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("\n");
}
private static boolean isPowerOfTwo(int x) // Funkcja zwraca liczby, ktore sa kolejna potega liczby 2
{
//return (x & (x-1)) ==0;
return (x!=0) && ((x&(x-1)) ==0);
}
public static void wyswietlBityPowerOfTwo(int tab[])
{
boolean [] bityPotegiDwa = new boolean[7];
System.out.println("Bity, ktorych indeks w tablicy jest kolejna potega liczby 2: ");
for (int i=0; i<tab.length; i++)
{
if (isPowerOfTwo(i))
{
int j = 0;
bityPotegiDwa[j+1] = isPowerOfTwo(i);
j++;
System.out.print(tab[i]+"("+i+")"+", ");
}
}
System.out.println("\n");
}
public static void podzielCiagNaParzysteGrupy(int tab[]) // Funkcja dzieli ciag bitow na 7 grup wg indeksow potegi 2
{
int [] gr1 = new int [56];
}
public static void main(String[] args) {
int [] tab = new int [110]; // Utworzenie tablicy jako globalnej zmiennej
losujBity(tab);
wyswietlBityPowerOfTwo(tab);
podzielCiagNaParzysteGrupy(tab);
}
}
Aucun commentaire:
Enregistrer un commentaire