samedi 28 septembre 2019

generating truth tables in php with position

i need to write code that generate truth table position. (php code)

for example:

result      a b c
  -         0 0 0 
c -         0 0 1 
b -         0 1 0 
cb -        0 1 1 
a -         1 0 0 
ca -        1 0 1 
ba -        1 1 0 
cba -       1 1 1 

i found this code (in java)

public class TruthTable {
    private static void printTruthTable(int n) {
        int rows = (int) Math.pow(2,n);

        for (int i=0; i<rows; i++) {
            for (int j=n-1; j>=0; j--) {
                System.out.print((i/(int) Math.pow(2, j))%2 + " ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        printTruthTable(3); //enter any natural int
    }
}

but i need the postion of the 1 and be able to generating TruthTable of 1,2,3...etc.

Aucun commentaire:

Enregistrer un commentaire