1

I need to write a function that takes as arguments an integer, which represents a row in a truth table, and a boolean array, where it stores the values for that row of the truth table.

Here is an example truth table

Row| A | B | C |
 1 | T | T | T |
 2 | T | T | F |
 3 | T | F | T |
 4 | T | F | F |
 5 | F | T | T |
 6 | F | T | F |
 7 | F | F | T |
 8 | F | F | F |

Please note that a given truth table could have more or fewer rows than this table, since the number of possible variables can change.

A function prototype could look like this

getRow(int rowNum, bool boolArr[]);

If this function was called, for example, as

getRow(3, boolArr[])

It would need to return an array with the following elements

|1|0|1|    (or |T|F|T|)  

The difficulty for me arises because the number of variables can change, therefore increasing or decreasing the number of rows. For instance, the list of variables could be A, B, C, D, E, and F instead of just A, B, and C.

I think the best solution would to be write a loop that counted up to the row number, and essentially changed the elements of the array like it was counting in binary. So that

1st loop iteration, array elements are 0|0|...|0|1|
2nd loop iteration, array elements are 0|0|...|1|0|

I can't for the life of me figure out how to do this, and can't find a solution elsewhere on the web. Sorry for all the confusion and thanks for the help

FutureShocked
  • 779
  • 1
  • 10
  • 26
  • Where does the formula itself come into play here? Is the goal to figure out what the individual variable assignments would be for a given row of the table, or how to parse the formula to evaluate? – templatetypedef Jan 21 '15 at 03:56
  • @templatetypedef The end goal is to parse the formula with each possible truth value for any number of variables. In essence, to check each row of a truth table to see if it satisfies the formula. That's why it's important to be able to generate each row of the truth table separately. For example if a,b, and c are variables, I need to check each combination of true and false for the three to see which combinations cause the entire formula to be true. – FutureShocked Jan 21 '15 at 04:04
  • Please try to improve the problem statement. You mention `genTruth` without explaining what the function should compute and then skip to bitshifts. It would help with your explanation if you first stated the problem clearly and then explained your approach. – Pradhan Jan 21 '15 at 04:47
  • @Pradhan If there's one thing I'm learning quickly on this site, it's how to ask an effective question. Is my edited approach any better? – FutureShocked Jan 21 '15 at 05:13
  • Yes, this is much better! Is there a specific reason the prototype needs to be `getRow(rowNum, boolArr[]);`? [C-style arrays decay to pointers](http://en.cppreference.com/w/cpp/language/array#Array_to_pointer_decay) when used as a function argument. If you know the size of the array at compile time, it is much easier to work with [`std::array`](http://en.cppreference.com/w/cpp/container/array). If you need to determine the size at runtime, you could use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) and your prototype could be `getRow(rowNum, vector, numVars)`. – Pradhan Jan 21 '15 at 09:25
  • @Pradhan No, that was just the suggestion by my professor. I'm still acclimating myself to vectors, as I just started learning them a few days ago. – FutureShocked Jan 21 '15 at 19:17

1 Answers1

1

Ok now that you rewrote your question to be much clearer. First, getRow needs to take an extra argument: the number of bits. Row 1 with 2 bits produces a different result than row 1 with 64 bits, so we need a way to differentiate that. Second, typically with C++, everything is zero-indxed, so I am going to shift your truth table down one row so that row "0" returns all trues.

The key here is to realize that the row number in binary is already what you want. Take this row (having shifted down the 4 to 3):

3 | T | F | F |

3 in binary is 011, which inverted is {true, false, false} - exactly what you want. We can express that using bitwise-or as the array:

{!(3 | 0x4), !(3 | 0x2), !(3 | 0x1)}

So it's just a matter of writing that as a loop:

void getRow(int rowNum, bool* arr, int nbits)
{
    int mask = 1 << (nbits - 1);
    for (int i = 0; i < nbits; ++i, mask >>= 1) {
        arr[i] = !(rowNum & mask);
    }
}
Barry
  • 286,269
  • 29
  • 621
  • 977
  • Sorry about that, I tried clarifying a bit by renaming the variables in my OP to letters instead of numbers. I don't think that's right, based on what I could understand of the cplusplus reference page for bitset. It looks to me like your version of the function is returning the entire truth table, but I need to be able to generate a single row of it at a time. The first row of a truth table in two variables would be [T|T], the second being [T|F], the third [F|T] and the fourth [F|F]. My apologies if that sounds at all condescending, just trying to clarify as much as possible. – FutureShocked Jan 21 '15 at 04:13
  • @FutureShocked If you want just the truth table, that's just the `bits` variable in my answer. That will be the values in the `rowNum`th row. – Barry Jan 21 '15 at 04:22
  • This only returns a single boolean value though, doesn't it? Even ignoring that rowNum would be larger than 8 for any number of variables larger than 3, I still need the function to return the boolean value for each variable in the row. – FutureShocked Jan 21 '15 at 04:35
  • @FutureShocked Ok much clearer question - I rewrote my answer. – Barry Jan 21 '15 at 12:18