Jump to content

Recommended Posts


Posted (edited)

That's not enough info. Truth table for what logic? eg.

Truth table for 2^2 OR:

0 or 0 = 0

0 or 1 = 1

1 or 0 = 1

1 or 1 = 1

Pseudocode summary:

if any true then true

for AND:

0 and 0 = 0

0 and 1 = 0

1 and 0 = 0

1 and 1 = 1

if all true then true

for XOR:

0 xor 0 = 0

0 xor 1 = 1

1 xor 0 = 1

1 xor 1 = 0

if one and only one true then true

etc.

Edited by dman
Posted

Hi,

An algorithm has to be devised to fill the truth table for n variables as 2^n combination.

for example,

if i give n as 3, then

i need combinations as

000

001

010

100

110

101

011

111

Thanks and regards.

Posted (edited)

well, look at example above.

you need to write a loop to test n iterations of variable according to summary logic above.

ie. for or 2^3... loop three times and test variable each time. as soon as you find a true the truth table entry is true.

do same for other logic gates.

or are you just trying to fill in the conditions?

first get loop count 2^n -1

then fill in first row of all 0's

then start loop

read from left.

if it is 0 make it 1 and exit loop

If it is 1 make it zero and repeat test on next from left.

etc.

Edited by dman
Posted

Hi,

the code below prints out every permutation of a 2^n value (just adapt it to your needs, i've justed hacked this without much testing)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct binnum
{
int* pBinNum;
int nCount;
}binnum;

int  DecToBin( int nNum, binnum* pBinVal );

int main( int argc, char* argv[] )
{
int nNumTruth, nUpperLimit, nRun, nCurNum;
binnum* pBinVal = NULL;

pBinVal = ( binnum*) malloc( sizeof( binnum ));
pBinVal->pBinNum = ( int* ) malloc( sizeof( int ) * 100 );

if( argc != 2 ) {
 fprintf( stderr, "%s: give a num as option!\n", argv[0] );
 exit( -1 );
} else {
 nNumTruth = atoi( argv[1] );
}

nUpperLimit = ( int ) pow(( int ) 2, ( int ) nNumTruth );

for( nCurNum = 0; nCurNum < nUpperLimit; nCurNum ++ ) {
 DecToBin( nCurNum, pBinVal );
 for( nRun = 0; nRun < pBinVal->nCount; nRun ++ ) {
 fprintf( stdout, "%d", pBinVal->pBinNum[nRun]  );
 }
 fprintf( stdout, "\n" );
}

return 0;
}

int  DecToBin( int nNum, binnum* pBinVal )
{
int nTemp = nNum, nMod = 0, nIndex = 0, nCountIndex = 0;
int nArray[100];

while( nTemp >= 1 ) {
 nMod = ( nTemp % 2 );
 nTemp = nTemp / 2;

 nArray[nIndex] = nMod;
 nIndex ++;
}

pBinVal->nCount = nIndex;

for( nIndex--; nIndex >= 0; nIndex -- ) {
 pBinVal->pBinNum[nCountIndex] = nArray[nIndex];
 nCountIndex ++;
}

return 0;
}

Hope that helps,

Egon

Posted (edited)

Nice Egon,

But I think this is ootsoo's homework from school. You TOO helpful.

Edited by dman

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...