0% found this document useful (0 votes)
59 views3 pages

Truth Table Generator

Uploaded by

BenjaminCoetzee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Truth Table Generator

Uploaded by

BenjaminCoetzee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Excel Truth Table Generator

If you need to generate a binary truth table of any size in Excel, this post is for you.

I have written a simple program in VBA that once executed, askes the user for the number of inputs required then
generates the truth table for all inputs.

The Excel sheet is shown below:

Clicking the ‘Generate’ button, executes the code below:

Code

Sub GenerateTTable()
'Generates a truth table of user defined size (iInputs)

Dim iInputs As Integer '# of inputs


Dim iRows As Integer '# rows per input
Dim iRowNo As Integer 'Current row #
Dim dZeros As Double 'Number of adjacent zeros per input

Sheet1.Rows("10:" & Rows.Count).ClearContents 'Clears all sheet contents starting from row 10

iInputs = InputBox("Enter Number of Inputs: ", "Enter a Number")


iRows = 2 ^ iInputs
iRowNo = 0

For i = 1 To iInputs

INTERNAL
Sheet1.Cells(10, i).Value = "In " & i 'Populate input header numbers
dZeros = (1 / (2 ^ i)) * iRows

Do While iRowNo < iRows


For k0 = 1 To dZeros
Sheet1.Cells(10 + iRowNo + 1, i).Value = 0
iRowNo = iRowNo + 1

Next k0

For k1 = 1 To dZeros
Sheet1.Cells(10 + iRowNo + 1, i).Value = 1
iRowNo = iRowNo + 1

Next k1
Loop
iRowNo = 0

Next i
End Sub

Explanation of Code

The subroutine GenerateTTable() can be stored in any model as shown below:

This subroutine is tied to the Generate button, when the Generate button is pressed three integer variables and one
double variable are declared namely: iInputs, iRows, iRowNo and dZeros.

iInputs is used to store the user defined number of inputs. This variable is populated using the InputBox function, which
requests a numerical value from the user.

iRows stores the number of rows or possible combinations per input. This value is found by calculating 2 to the power of
the input number (2 ^ iInputs).

INTERNAL
iRowNo represents the current row number the program is looking at when populating the truth table

dZeros represents the number of zeros that are grouped together per input. This number changes per input and is found
as follows:

dZeros = (1 / (2 ^ [Current Input])) * iRows

So if you have a truth table with 3 inputs, dZeros will equal four for input 1, two for input 2 and 1 for input 1. This is
shown graphically in the truth table below:

Before the table is populated, any preexisting data is cleared. In this case any data stored from row 10 and below is
erased or cleared.

The initial for loop i, runs the remaining code through each input generating the input label then the zero and one
combinations.

The zero and one combinations are populated using a do while loop which keeps track of the current row number. Two
for are executed to populate the sheet with either zeros or ones.

INTERNAL

You might also like