Hi everyone, its been a while since i last posted, i started teaching myself C a while ago and i'm trying to get back to it. Been really busy at work with fun languages like Ocean!
Anyway, i like to learn by having a project and working towards it. So i thought i would try to write a program which could display a truth table given a boolean expression.
My first stumbling block has been dynamic arrays. I searched through the forums and found references to malloc(). I'm not asking these to be explained but i thought now would be a good time to ask if anyone has a better method of how to go about this? I don't know how well C deals with binary.
This is what i have so far:
of course this doesn't work, cause i'm stupid and didn't know you couldn't do that with arrays. This is only the part which will walk through the binary representation of 0 to 15 (for example)Code:
int x, i;
x = 4; //width of array
max_value = 1;
for (i = 0; i < 2; i++)
max_value *= x; //Given 'x' binary bits, max_value is all bits set to 1 +1
printf("max_value = %d\n", max_value);
for (i = max_value - 1; i >= 0; i--) //for each value
count = max_value / 2; //MSB
printf("i = %d\n", i);
bit = 0;
reg = i;
while(count > 0)
printf("count = %d\n", count); //Count corresponds to value of each bit.
printf("reg = %d\n", reg);
bit = x - bit;
printf("bit = %d\n", bit);
if (reg >= count) //if value is greater than or equal to
//corresponding bit, set it
inputs[bit] = 1;
reg = reg - count;
else //or not
inputs[bit] = 0;
bit = bit++; //move to next bit
count = count / 2; //and corresponding value
Sorry for the long post. Your help would be appreciated.
Also, does anyone have any ideas for projects? Maybe someone wants to set me a not to difficult task?