Функция отображения нечетного cout - PullRequest
1 голос
/ 07 мая 2020

Я пытаюсь убедиться, что мой код работает правильно, поэтому мои функции bool выводят истинное или ложное значение, которое они возвращают. Однако моя функция checkColSum, которая проверяет, добавляет ли каждый столбец в моем параллельном массиве 15, печатает три истинности и ложь, когда я ввожу числа:

4 9 2 3 5 7 8 1 6

Буду очень признателен за любую помощь по этому вопросу. Я просто не могу понять, что происходит не так.

#include <iostream>
using namespace std;

// Global constants 
const int ROWS = 3;  // The number of rows in the array
const int COLS = 3;  // The number of columns in the array
const int MIN = 1;  // The value of the smallest number
const int MAX = 9;  // The value of the largest number

// Function prototypes
bool isMagicSquare(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
bool checkRange(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size, int min, int max);
bool checkUnique(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
bool checkRowSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
bool checkColSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
bool checkDiagSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
void fillArray(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
void showArray(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);

int main()
{

    /* Define a Lo Shu Magic Square using 3 parallel arrays corresponding         to each row of the grid */
    int magicArrayRow1[COLS], magicArrayRow2[COLS], magicArrayRow3[COLS];
    // Your code goes here
    int repeat = 1;

    while (repeat == 1) {
        fillArray(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3);
        showArray(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3);

        /*TESTING DELETE BEFORE TURNING IN*/
        checkRange(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3, 1, 9); //works
        cout << endl;
        checkUnique(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3); //doesnt work
        cout << endl;
        checkRowSum(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3);//works
        cout << endl;
        checkColSum(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3); //works?? maybe??

        /*TESTING DELETE BEFORE TURNING IN*/

        if (isMagicSquare(magicArrayRow1, magicArrayRow2, magicArrayRow3, 3) == true) {
            cout << endl << "This is a Lo Shu magic square.";
        }
        else {
            cout << endl << "This is not a Lo Shu magic square.";
        }
        cout << endl << "Would you like to try again? Enter 1 for yes or 0  for no: ";
        cin >> repeat;
    }

    return 0;
}
// Function definitions go here
bool isMagicSquare(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size)
{
    bool trueFalse = false;
    if (checkRange(arrayRow1, arrayRow2, arrayRow3, 3, 1, 9) == true && checkUnique(arrayRow1, arrayRow2, arrayRow3, 3) == true) {
        if (checkRowSum(arrayRow1, arrayRow2, arrayRow3, 3) == true && checkColSum(arrayRow1, arrayRow2, arrayRow3, 3) == true) {
            if (checkDiagSum(arrayRow1, arrayRow2, arrayRow3, 3) == true) {
                trueFalse = true;
            }
        }
    }

    return trueFalse;
}

bool checkRange(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size, int min, int max)
{
    bool inRange = true;
    int count = 0;

    while (inRange && count < size) {
        if (arrayRow1[count] < min || arrayRow1[count] > max) {
            inRange = false;
        }
        count++;
    }

    while (inRange && count < size) {
        if (arrayRow2[count] < min || arrayRow2[count] > max) {
            inRange = false;
        }
        count++;
    }

    while (inRange && count < size) {
        if (arrayRow3[count] < min || arrayRow3[count] > max) {
            inRange = false;
        }
        count++;
    }

    if (inRange == true) {// TEST
        cout << "true";
    }
    if(inRange == false) {
        cout << "false";
    }
    return inRange;
}

bool checkUnique(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size)
{
    bool isUnique = false;
    int count = 0;

    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;

    for (int i = 0; i < size; i++)
    { //checks row 1
        if (arrayRow1[i] == 1) { //checks if row one contains the value 1 in any position
            one++;
        }
        if (arrayRow1[i] == 2) {
            two++;
        }
        if (arrayRow1[i] == 3) {
            three++;
        }
        if (arrayRow1[i] == 4) {
            four++;
        }
        if (arrayRow1[i] == 5) {
            five++;
        }
        if (arrayRow1[i] == 6) {
            six++;
        }
        if (arrayRow1[i] == 7) {
            seven++;
        }
        if (arrayRow1[i] == 8) {
            eight++;
        }
        if (arrayRow1[i] == 9) {
            nine++;
        }


    }


    for (int i = 0; i < size; i++) { //checks row 2
        if (arrayRow1[i] == 1) { //checks if row two contains the value 1 in any position
            one++; //if the value 1 is present, a count is added
        }
        if (arrayRow2[i] == 2) {
            two++;
        }
        if (arrayRow2[i] == 3) {
            three++;
        }
        if (arrayRow2[i] == 4) {
            four++;
        }
        if (arrayRow2[i] == 5) {
            five++;
        }
        if (arrayRow2[i] == 6) {
            six++;
        }
        if (arrayRow2[i] == 7) {
            seven++;
        }
        if (arrayRow2[i] == 8) {
            eight++;
        }
        if (arrayRow2[i] == 9) {
            nine++;
        }
    }

    for (int i = 0; i < size; i++) { //checks row 1
        if (arrayRow3[i] == 1) { //checks if row one contains the value 1 in any position
            one++;
        }
        if (arrayRow3[i] == 2) {
            two++;
        }
        if (arrayRow3[i] == 3) {
            three++;
        }
        if (arrayRow3[i] == 4) {
            four++;
        }
        if (arrayRow3[i] == 5) {
            five++;
        }
        if (arrayRow3[i] == 6) {
            six++;
        }
        if (arrayRow3[i] == 7) {
            seven++;
        }
        if (arrayRow3[i] == 8) {
            eight++;
        }
        if (arrayRow3[i] == 9) {
            nine++;
        }
    }

    if (one == 1 && two == 1 && three == 1 && four == 1 && five == 1 && six == 1 && seven == 1 && eight == 1 && nine == 1) { // checks that every variable counting the repition of numbers 1-9 in the arrays is equal to one
        isUnique = true;
        cout << "True";
    }
    if (one != 1 || two != 1 || three != 1 || four != 1 || five != 1 || six != 1 || seven != 1 || eight != 1 || nine != 1) {

        isUnique = false;
        cout << "False";
    }

    return isUnique;
}

bool checkRowSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size)
{
    bool magicSquare = true; //variable declares whether it is a magic square or not
    int sumRow1 = 0; //sum of row1
    int sumRow2 = 0; //sum of row2
    int sumRow3 = 0; //sum of row3

    for (int i = 0; i < size; i++) {
        sumRow1 += arrayrow1[i];
    }

    for (int i = 0; i < size; i++) {
        sumRow2 += arrayrow2[i];
    }

    for (int i = 0; i < size; i++) {
        sumRow3 += arrayrow3[i];
    }

    if (sumRow1 == 15 && sumRow2 == 15 && sumRow3 == 15) {
        magicSquare = true;
        cout << "true";
    }
    else {
        magicSquare = false;
        cout << "false";
    }

    return magicSquare;
}

bool checkColSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size)
{
    bool magicSquare = false;

    int sumCol1 = 0;
    int sumCol2 = 0;
    int sumCol3 = 0;

    for (int i = 0; i < size; i++) { //iterates through the array
        if (i == 0) { //when i equals 1
            sumCol1 += arrayrow1[i]; 
        }
        if (i == 1) {
            sumCol2 += arrayrow2[i];
        }
        if (i == 2) {
            sumCol3 += arrayrow3[i];
        }
    }

    for (int i = 0; i < size; i++) {
        if (i == 0) {
            sumCol1 += arrayrow3[i];
        }
        if (i == 1) {
            sumCol2 += arrayrow1[i];
        }
        if (i == 2) {
            sumCol3 += arrayrow2[i];
        }
    }

    for (int i = 0; i < size; i++) {
        if (i == 0) {
            sumCol1 += arrayrow2[i];
        }
        if (i == 1) {
            sumCol2 += arrayrow3[i];
        }
        if (i == 2) {
            sumCol3 += arrayrow1[i];
        }
    }

    if (sumCol1 == 15 && sumCol2 == 15 && sumCol3 == 15) {
        magicSquare = true;
        cout << "true";
    }
    else {
        magicSquare = false;
    }


    return magicSquare;
}

bool checkDiagSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size)
{
    bool magicSquare = true;
    int sumDiag1 = 0;
    int sumDiag2 = 0;

    sumDiag1 = arrayrow1[0] + arrayrow2[1] + arrayrow3[2];
    sumDiag2 = arrayrow1[3] + arrayrow2[1] + arrayrow3[0];

    if (sumDiag1 == 15 && sumDiag2 == 15) {
        magicSquare = true;
        cout << "true";
    }
    else {
        magicSquare = false;
        cout << "false";
    }

    return magicSquare;
}

void fillArray(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size)
{
    int userInput;

    //fills row 1
    for (int i = 0; i < size; i++) { //iterates according to int size which is the number of columns/rows
        cout << "Enter the number for row 1, column " << i+1 << ": ";
        cin >> userInput;
        arrayRow1[i] = userInput;
        cout << endl;
    }

    //fills row 2
    for (int i = 0; i < size; i++) { 
        cout << "Enter the number for row 2, column " << i+1 << ": ";
        cin >> userInput;
        arrayRow2[i] = userInput;
        cout << endl;
    }

    //fills row 3
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the number for row 3, column " << i+1 << ": ";
        cin >> userInput;
        arrayRow3[i] = userInput;
        cout << endl;
    }
}

void showArray(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size)
{
    for (int i = 0; i < size; i++) { //displays row 1
        cout << arrayrow1[i] << " ";
    }
    cout << endl; //moves down one row

    for (int i = 0; i < size; i++) { //displays row 2
        cout << arrayrow2[i] << " ";
    }
    cout << endl; //moves down one row

    for (int i = 0; i < size; i++) { //displays row 3
        cout << arrayrow3[i] << " ";
    }
    cout << endl; //moves down one row
}
...