Home > OS >  How to fix output of truth table in C?
How to fix output of truth table in C?

Time:03-19

I'm trying to create a truth table in c, I have the left side built so i was able to show all the possibilities of the input numbers in a truth table. but i am having trouble getting the right output for the right side of the truth table. What I mean is we ask the user to write 1 or 0 and once we got trough all the row it should print the user's input on the right side of the truth table.

For example:

row 0:1

row 1:0
row 2:0
row 3:0
row 4:1
row 5:1
row 6:0
row 7:0

Output should look like this

0, 0, 0, : 1
0, 0, 1, : 0
0, 1, 0, : 0
0, 1, 1, : 0
1, 0, 0, : 1
1, 0, 1, : 1
1, 1, 0, : 0
1, 1, 1, : 0

As you can see, the input from the user should be on the right side of truth table

#include <stdio.h>


void increment(int truth_table[8][4], int f[8]){


for(int z=0; z<=1; z  ){
    
    for(int y=0;y<=1; y  ){
        
        for(int x=0; x<=1; x  ){
    
            for(int j = 0; j < 1; j = j   1){
            printf("%d  %d  %d  : %d\n", z,y,x,f[j]);
         }
      }
    }
  }
}


void right_side(int truth_table[8][4], int f[8]){


for(int i = 0; i < 8; i = i   1) {

  for(int j = 0; j < 1; j = j   1) {
     printf("row %d:",j);
     scanf(" %d", &f[j]);
   }
 }

  increment(truth_table,f);
}

int main(void) {

 int f[8];

 int truth_table[8][4] = {0};

 right_side(truth_table,f);

 return 0;
}

CodePudding user response:

There's no reason why you need to complicate this and drag in 2D arrays etc. These are bits, not int. I think the function you are looking for should simply be something straight-forward table look-up like this:

bool truthify (bool a, bool b, bool c)
{
  const bool truth_table[] = 
  {
    [0x00] = true,
    [0x01] = false,
    [0x02] = false,
    [0x03] = false,
    [0x04] = true,
    [0x05] = true,
    [0x06] = false,
    [0x07] = false,
  };

  uint8_t index = a<<2 | b<<1 | c<<0;
  return truth_table[index & 0x07];
}

Test code:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

bool truthify (bool a, bool b, bool c)
{
  const bool truth_table[] = 
  {
    [0x00] = true,
    [0x01] = false,
    [0x02] = false,
    [0x03] = false,
    [0x04] = true,
    [0x05] = true,
    [0x06] = false,
    [0x07] = false,
  };

  uint8_t index = a<<2 | b<<1 | c<<0;
  return truth_table[index & 0x07];
}

int main (void)
{
  for(size_t i=0; i<8; i  )
  {
    bool a = i&4;
    bool b = i&2;
    bool c = i&1;

    printf("%d, %d, %d, : %d\n", a, b, c, truthify(a,b,c));
  }
}

Output:

0, 0, 0, : 1
0, 0, 1, : 0
0, 1, 0, : 0
0, 1, 1, : 0
1, 0, 0, : 1
1, 0, 1, : 1
1, 1, 0, : 0
1, 1, 1, : 0

Now simply rewrite that to take some user input and pass the truth table as a parameter to the function and there you go.

CodePudding user response:

The problem is in the last j loop because you are printing just 0 and 1 but you want to print the elements of the array that you have inserted in function void righ-side()

The updated code is here as follows:

#include <stdio.h>


void increment(int truth_table[8][4], 
int f[8]){


for(int z=0; z<=1; z  ){

    for(int y=0;y<=1; y  ){
    
        for(int x=0; x<=1; x  ){

           // for(int j = 0; j < 8; j  ){
           printf("%d  %d  %d  : ", z,y,x);
           //  }
           printf("%d\n",f[record]);
             record;
    }
    }
 }
}


void right_side(int truth_table[8][4], 
int f[8]){
    for(int i = 0; i < 8; i = i   1) {
        printf("row %d of F%d output variable:", i,i);
        scanf(" %d", &f[i]);
    }
    increment(truth_table,f);
}

int main(void) {

  int f[8];

  int truth_table[8][4] = {0};

  right_side(truth_table,f);

  return 0;
}
  • Related