ootsoo Posted August 2, 2005 Share Posted August 2, 2005 Hi An algorithm has to be devised to fill the truth table for n variables as 2^n combination.Thanks and regards. Link to comment Share on other sites More sharing options...
dman Posted August 2, 2005 Share Posted August 2, 2005 (edited) That's not enough info. Truth table for what logic? eg.Truth table for 2^2 OR:0 or 0 = 00 or 1 = 11 or 0 = 11 or 1 = 1Pseudocode summary:if any true then truefor AND:0 and 0 = 00 and 1 = 01 and 0 = 01 and 1 = 1if all true then truefor XOR:0 xor 0 = 00 xor 1 = 11 xor 0 = 11 xor 1 = 0if one and only one true then trueetc. Edited August 2, 2005 by dman Link to comment Share on other sites More sharing options...
ootsoo Posted August 2, 2005 Author Share Posted August 2, 2005 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, theni need combinations as000 001010100110101011111Thanks and regards. Link to comment Share on other sites More sharing options...
dman Posted August 2, 2005 Share Posted August 2, 2005 (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 -1then fill in first row of all 0'sthen start loopread from left.if it is 0 make it 1 and exit loopIf it is 1 make it zero and repeat test on next from left.etc. Edited August 2, 2005 by dman Link to comment Share on other sites More sharing options...
egrath Posted August 2, 2005 Share Posted August 2, 2005 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 Link to comment Share on other sites More sharing options...
dman Posted August 2, 2005 Share Posted August 2, 2005 (edited) Nice Egon, But I think this is ootsoo's homework from school. You TOO helpful. Edited August 2, 2005 by dman Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now