0

I'm working on magic square testing program, which determines if the values in a two dimensional array qualify as a magic square. What this means is that every row, column, and diagonal add up to the same values, and more importantly for my case, that each number is unique and not repeated.

I've figured everything else out, except for how to determine if the values are unique. I thought it would be a good idea to create a separate single dimension array, and feed the values from the two dimensional array into it. Then I thought I would test the values against each other to see if any repeat. It seems my logic is wrong on this somewhere, but I'm not sure where. Here is what I've come up with so far:

#include <iostream>
using namespace std;
const int D=3;
void evalSquare(int [][D], int, int);

int main()
{
     int validSquare[D][D] = {{4, 9, 2},
                              {3, 5, 7},
                              {8, 1, 6}};
     int invalidSquare[D][D] = {{1, 1, 1},
                                {1, 1, 1},
                                {1, 1, 1}};

    cout << "Inputing square one." << endl;
    evalSquare(validSquare, 0, 0);
    cout << "Inputting square two." << endl;
    evalSquare(invalidSquare, 0, 0);

}

void evalSquare(int square[D][D], int R, int C)
    {
        int n=9, repeatedValuesTest[n], testValue, t;
        bool magicSquare = false;


        for(R=0, n=0; R<3; R++)
        {
            for(C=0; C<3; C++)
            {
                repeatedValuesTest[n] = square[R][C];
                n++;
            }
        }

        cout << repeatedValuesTest[0] << repeatedValuesTest[1] <<   
        repeatedValuesTest[2] << endl;
        cout << repeatedValuesTest[3] << repeatedValuesTest[4] << 
        repeatedValuesTest[5] << endl;
        cout << repeatedValuesTest[6] << repeatedValuesTest[7] << 
        repeatedValuesTest[8] << endl;

        for(n=0; n<9; n++)
        {
            testValue = repeatedValuesTest[n];
            for(t=n; t<9; t++)
            {
                 if (testValue==repeatedValuesTest[t])
                 {
                     magicSquare = false;
                 }

             }

        if(magicSquare)
        {
            cout << "This is a magic square" << endl;
        }
        else
            cout << "This is not a magic square" << endl;

    }

And here is what is being printed out.

Program Run Results

s you can see, for some reason, after the values to the repeatedValuesTest array, it becomes gibberish. Is this because I am passing from a two dimensional array to a one dimensional array? How would I go about doing this in a way that would work?

Also, I realize there are probably far better ways to do what I'm trying to do, using vectors or something else, but I'm in a basic CompSci class, and arrays and functions are as far as we've gotten, so that's what we have to use for this project.

Thanks in advance.

EDIT: I realized I had an error in how I was printing out the contents of the repeatedValuesTest array. I fixed that, and now it prints out as it should, which would seem to indicate the error is not in passing it from a multi-dimension array, but in how I am testing it, since both arrays come up as invalid at the end.

0 Answers0