0
#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;
}

これは私のコードです。これは魔方陣を計算します。私が得られない唯一のことは、ループするたびに数値が切り替わると思われることですが、ループするたびに同じ数値が表示されるため、基本的に無限ループになります...私が求めているのは、数値を取得するためのヘルプですループするたびに変更します。事前にどうもありがとうございました。

4

1 に答える 1

1

コードを記述するときは、他のコードと統合する前に、新しい機能を個別に開発する必要があります。

乱数ジェネレーターを使用していたとき、次のことに気付いたはずです。

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

乱数発生器をリセットします。そして、メインループを通過するたびにこれを行うため、毎回同じ「乱数」が得られます。

その行をループの外に移動します。

于 2015-04-30T01:37:02.113 に答える