I had to do something similar recently, except that the project was supposed to generate a truth table for a given logical expression. This is what I came up with to assign independent variables their truth values.
column = 0;
while (column < numVariables)
{
state = false;
toggle = (short) Math.pow(2, numVariables - column - 1);
row = 1;
while (row < rows)
{
if ((row -1)%toggle == 0)
state = !state;
if (state)
truthTable[row][column] = 'T';
else
truthTable[row][column] = 'F';
row++;
}
column++;
}
This assumes your first line is populated with variable names and subexpressions. The math may change a bit if you want to start at line 0.
This bit ....
if ((line -1)% toggle == 0)
will ....
if (line% toggle == 0)
source
share