Having trouble getting code to draw kmap based on user input, and grouping them based on SOP and POS then writing them in simplified and original

31 views Asked by At

Can you fix this code for me?

#include

#include

#include

#include

#include

using namespace std;

// Function to print the Karnaugh Map

void printKMap(const vector<vector>& kMap, const vector& variables) {

int numRows = kMap.size();

int numCols = kMap[0].size();

cout << "========K Map========" << endl;

cout << "\\";

for (int j = 0; j < numCols; j++) {

    cout << variables[j] << " ";

}

cout << "|" << endl;

for (int i = 0; i < numRows; i++) {

    for (int j = 0; j < numCols; j++) {

        if (j == 0) {

            cout << i << " | ";

        }

        cout << kMap[i][j] << " ";

    }

    cout << endl;

}

}

// Function to simplify the groups and generate simplified expression

string simplifyGroups(const vector<vector>& groups, const vector& variables) {

stringstream ss;

for (int i = 0; i < groups.size(); i++) {

    bool isFirstTerm = true;

    for (int j = 0; j < variables.size(); j++) {

        if (groups[i][j] != -1) {

            if (!isFirstTerm) {

                ss << " + ";

            }

            if (groups[i][j] == 0) {

                ss << variables[j] << "'";

            } else {

                ss << variables[j];

            }

            isFirstTerm = false;

        }

    }

}

return ss.str();

}

// Function to generate the original expression

string getOriginalExpression(const vector<vector>& groups, const vector& variables) {

stringstream ss;

for (int i = 0; i < groups.size(); i++) {

    bool isFirstTerm = true;

    for (int j = 0; j < variables.size(); j++) {

        if (groups[i][j] != -1) {

            if (!isFirstTerm) {

                ss << " + ";

            }

            if (groups[i][j] == 0) {

                ss << variables[j] << "'";

            } else {

                ss << variables[j];

            }

            isFirstTerm = false;

        }

    }

}

return ss.str();

}

int main() {

char runAgain = 'Y';

while (runAgain == 'Y') {

    int numVariables;

    cout << "Enter the number of variables: ";

    cin >> numVariables;

    vector<string> variables(numVariables);

    cout << "Enter the variable letters: ";

    for (int i = 0; i < numVariables; i++) {

        cin >> variables[i];

    }

    int numLocations;

    cout << "Enter the number of locations of the 1 values: ";

    cin >> numLocations;

    int numRows = pow(2, numVariables);

    int numCols = numVariables;

    vector<vector<int>> kMap(numRows, vector<int>(numCols, 0));

    cout << "Enter the locations of the 1 values: ";

    for (int i = 0; i < numLocations; i++) {

        int location;

        cin >> location;

        kMap[location >> numVariables][location & ((1 << numVariables) - 1)] = 1;

    }

    printKMap(kMap, variables);

    vector<vector<int>> groups;

    for (int i = 0; i < numRows; i++) {

        for (int j = 0; j < numCols; j++) {

            if (kMap[i][j] == 1) {

                vector<int> group(numCols, -1);

                group[j] = 1;

                for (int k = j + 1; k < numCols; k++) {

                    if (kMap[i][k] == 1) {

                        group[k] = 1;

                    }

                }

                groups.push_back(group);

            }

        }

    }

    cout << endl;

    for (int i = 0; i < groups.size(); i++) {

        cout << "Group " << i + 1 << ": ";

        for (int j = 0; j < numCols; j++) {

            if (groups[i][j] != -1) {

                cout << pow(2, j) * groups[i][j] << " ";

            }

        }

        cout << endl;

    }

    string simplifiedExpr = simplifyGroups(groups, variables);

    cout << "Simplified Expression: F(";

    for (int i = 0; i < numVariables; i++) {

        cout << variables[i];

        if (i < numVariables - 1) {

            cout << ",";

        }

    }

    cout << ") = " << simplifiedExpr << endl;

    string originalExpr = getOriginalExpression(groups, variables);

    cout << "Original Expression: F(";

    for (int i = 0; i < numVariables; i++) {

        cout << variables[i];

        if (i < numVariables - 1) {

            cout << ",";

        }

    }

    cout << ") = " << originalExpr << endl;

    cout << "Run again (Y/N): ";

    cin >> runAgain;

    runAgain = toupper(runAgain);

}

return 0;

}

This is what my professor wants the output to look like but I just can't get it right.

Enter the variable letters: WXYZ

Enter the location of the 1 values: 0 2 4 5 6 7 8 12

========K Map=========

\YZ|

WX \ | 00 01 11 10

-----+----------------

00 | 1 | 0 | 0 | 1 |

-----+---+---+---+----

01 | 1 | 1 | 1 | 1 |

-----+---+---+---+----

11 | 1 | 0 | 0 | 0 |

-----+---+---+---+----

10 | 1 | 0 | 0 | 0 |


Group 1: 0 4 8 12

Simplification of group 1 -> Y'Z'

Group 2: 4 5 6 7

Simplification of group 2 -> W'X

Group 3: 0 2 4 6

Simplification of group 3 -> W'Z'

Simplified Expression: F(W,X,Y,Z) = Y'Z' + W'X + W'Z'

Original Expression: F(W,X,Y,Z) = W'X'Y'Z' + W'X'YZ' + W'XY'Z' + W'XY'Z + W'XYZ'

  • W'XYZ + WX'Y'Z' + WXY'Z'

Run again (Y/N): N

I tried getting the output my professor wanted but the kmap doesn't draw right which I can't test my groupings or Simplification if the groupings cause I can't get the kmap to draw.

0

There are 0 answers