Ventus
Ventus

Reputation: 81

Best data set for truth table to compute and reduce Sum of Products in C#

so im trying to create a truth table in C# so that i can preform some boolean algebra on it. it is suppose to be a three variable truth table with 8 rows. so far right now im trying to use a string array of an array to input the truth table.

string[][] truthTable = new string[8][];
            truthTable[0] = new string[2] { "1" , "000"};
            truthTable[1] = new string[2] { "0", "001" };
            truthTable[2] = new string[2] { "0", "010" };
            truthTable[3] = new string[2] { "0", "011" };
            truthTable[4] = new string[2] { "0", "100" };
            truthTable[5] = new string[2] { "1", "101" };
            truthTable[6] = new string[2] { "1", "110" };
            truthTable[7] = new string[2] { "1", "111" };





            for (int i = 0; i < truthTable.Length; i++)
            {
                // print out strings  that have "1 as first element"
                if (truthTable[i].GetValue(i) == "1" )

                {
                    Console.WriteLine(truthTable[i]);

                }

            }

what I want to do right now is print out the arrays that have "1" as there first element. the console out put should be something like "1""000" for the first array for example, and it should only print the other three arrays that have "1" as well. but right now it gives me an out of bounds error and doesn't print anything.

Is this a good way to get started with a truth table to compute the sum of products or is there a better way to implement it in C#?

Upvotes: 0

Views: 494

Answers (1)

Bondolin
Bondolin

Reputation: 3121

One simple implementation would be to use a Dictionary<string, string> instead. The string key would hold your three variable values, and the second string value would hold the corresponding truth value:

var truthTable = new Dictionary<string, string>
{
    { "000", "1" },
    { "001", "0" },
    { "010", "0" },
    { "011", "0" },
    { "100", "0" },
    { "101", "1" },
    { "110", "1" },
    { "111", "1" },
};

foreach (var keyValue in truthTable)
{
    // print out strings that have value of "1"
    if (keyValue.Value == "1")
    {
        Console.WriteLine(keyValue.Key);
    }
}

Though perhaps more in keeping with the domain of interest, you might consider just using a Tuple of bools for the instead of a string for the variables key, and a bool instead of a string for the value:

var truthTable = new Dictionary<Tuple<bool, bool, bool>, bool>
{
    { new Tuple<bool, bool, bool>(false, true, false), true },
    { new Tuple<bool, bool, bool>(false, true, true), false },
    { new Tuple<bool, bool, bool>(false, false, false), false },
    { new Tuple<bool, bool, bool>(false, false, true), false },
    { new Tuple<bool, bool, bool>(true, true, false), false },
    { new Tuple<bool, bool, bool>(true, true, true), true },
    { new Tuple<bool, bool, bool>(true, false, false), true },
    { new Tuple<bool, bool, bool>(true, false, true), true },
};

foreach (var keyValue in truthTable)
{
    // print out strings that have true value
    if (keyValue.Value)
    {
        Console.WriteLine(keyValue.Key);
    }
}

Update: using Linq for Dictionary

You can roughly approximate the Dictionary as a List of KeyValuePair tuples. This means you can use all the Linq functionality available to any Collection -- e.g. the above foreach loop could use the Where Linq extension method and be simplified to the following:

// print out strings that have true value
var trueKeyValuesList = truthTable.Where(kv => kv.Value).ToList();
foreach (var keyValue in trueKeyValuesList)
{
    Console.WriteLine(keyValue.Key);
}

In this example, trueKeyValuesList is just that -- List<KeyValuePair<Tuple<bool, bool, bool>, bool>> (I <3 var :P ). If just wanted a list of the Tuple values, you could use the Select Linq method (which behaves similarly to python's map) together with Where:

// print out strings that have true value
var trueValueKeys = truthTable
    .Where(kv => kv.Value)
    .Select(kv => kv.Key)
    .ToList();
foreach (var boolTuple in trueValueKeys)
{
    Console.WriteLine(boolTuple);
}

Upvotes: 1

Related Questions