Dust
Dust

Reputation: 25

Magic square code

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

// Functions used in the program.
bool uniqueCheck(int arr[][3], int num);
bool magicSquare(int arr[][3]);
int rTotal(int arr[][3], int row);
int cTotal(int arr[][3], int col);
int dTotal(int arr[][3], bool left);
void display(int arr[][3]);

int main()
{
    int arr[3][3]; //makes the array 3x3 makes it into the square
    int test[3][3] = {2, 7, 6, 9, 5, 1 , 4 , 3 ,8}; //numbers from 1-9 in order of magic square.

    while (1)
    {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                arr[i][j] = 0;// nested for loop the i is for rows and the j is for columns

        srand((unsigned)time(NULL));// generates random numbers
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                while (1)
                {
                    int num = (rand() % 9) + 1; // Random function will spit out random numbers from 1-9.
                    if (uniqueCheck(arr, num))
                    {
                        arr[i][j] = num;
                        break;
                    }
                }
            }
        }

        display(arr);
        if (magicSquare(arr))//If the magic square array is an actual magic square than this outputs
        {
            cout << "This is the Magic Square !" << endl;
            break;
        }
        else //if not than it'll keep looping this until the top one is displayed
            cout << "This is Not the Magic Square !" << endl;

    }
    return 0;
}

// check if the random number generated is a unique number
bool uniqueCheck(int arr[][3], int num)
{
    for (int k = 0; k < 3; k++)
        for (int i = 0; i < 3; i++)
            if (arr[k][i] == num)
                return false;
    return true;
}

bool magicSquare(int arr[][3]) //This will check if the number presented (randomly) correspond with the magic square numbers.
{
    int sum = dTotal(arr, true); // Will check the sum of the diagonal.

    if (sum != dTotal(arr, false))
        return false;

    for (int i = 0; i < 3; i++)
    {
        if (sum != rTotal(arr, i)) // This will check each row and see if its true or false.
            return false;

        if (sum != cTotal(arr, i)) // This will check each column and see if its true or false.
            return false;
    }

    return true;
}

int rTotal(int arr[][3], int row) // This will calculate the sum of one row at a time.
{
    int sum = 0;
    for (int i = 0; i < 3; i++)
        sum += arr[row][i];
    return sum;
}

int cTotal(int arr[][3], int col) // This will calculate the sum of one column at a time.
{
    int sum = 0;
    for (int i = 0; i < 3; i++)
        sum += arr[i][col];
    return sum;
}

int dTotal(int arr[][3], bool left) // This will calculate the sum of diagonal. if the left is true, it will calculate from the left to the right diagonal. If false it will calculate from the right to the left diagonal.
{
    int sum = 0;
    if (left == true)
    {
        for (int i = 0; i < 3; i++)
            sum += arr[i][i];
        return sum;
    }

    for (int i = 0; i < 3; i++)
        sum += arr[i][3 - i - 1];
    return sum;
}

void display(int arr[][3]) //This will display the array.
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            cout << arr[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}

This is my code. This calculates a magic square. The only thing I cannot get is my numbers are suppose to switch each time it loops but it shows the same number each time it loops which basically make it an infinite loop...what I'm asking for is help on getting the numbers to change each time it loops. Thank you very much in advance.

Upvotes: 0

Views: 5120

Answers (1)

Beta
Beta

Reputation: 99094

When writing code, you should develop new functionality in isolation, before integrating it with other code.

When you were working with the random number generator, you should have noticed that this:

srand((unsigned)time(NULL));// generates random numbers

resets the random number generator. And since you do this in every pass through the main loop, you get the same "random" numbers every time.

Move that line up, outside the loop.

Upvotes: 1

Related Questions