简体   繁体   English

使用具有用户输入维度的 C++ 中动态分配的二维数组生成 NxN 幻方

[英]Generating an NxN magic square using a dynamically allocated 2D array in C++ with user-input dimension

I'm trying to generate and solve an NxN odd-numbered magic square through dynamic memory allocation but whenever I run the code, it displays nothing on the terminal and the programme ends.我正在尝试通过动态 memory 分配生成并解决 NxN 奇数幻方,但每当我运行代码时,它在终端上什么都不显示,程序结束。 I reckon it has something to do with the dynamic allocation of the 2D array, as when I make a normal NxN array with a constant size, the programme runs fine.我认为它与二维数组的动态分配有关,因为当我制作一个具有恒定大小的普通 NxN 数组时,程序运行良好。 I'd appreciate any help regarding this!对此有任何帮助,我将不胜感激!

#include<bits/stdc++.h>
#include<cmath>

using namespace std;

void calcuateMagicSquare(int N)

{

    int **Array;
    Array=new int*[N];
        for(int i=0; i<N; i++)
        {
            Array[i]=new int[N];
        }
        memset(Array, 0, sizeof(Array));
    int nSquare=N*N;
    int i=N/2;
    int j=N-1;
        for(int k=1; k<=nSquare;)
        {
            if(i==-1 && j==N)
            {
                j=N-2;
                i=0;
            }
            else
            {
                if(j==N)
                {
                    j=0;
                }
                if(i<0)
                {
                    i=N-1;
                }
            }
            if(Array[i][j])
            {
                j=j-2;
                i++;
                continue;
            }
            else
            {
                Array[i][j]=k++;
            }
            j++;
            i--;
        }
    int SolutionMagicSquare=N*(N*N+1)/2;
        cout << "Solution of the magic Square: " << SolutionMagicSquare << endl;
        cout << "MAGIC SQUARE: \n" << endl;
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
            {
                cout << setw(4) << Array[i][j] << " ";
                cout << endl;
            }
        }

}

int main()

{

    int N;
        cout << "Please enter the dimension of the magic square:" << endl;
        cin >> N;
        calcuateMagicSquare(N);

}

This isn't too bad.这还不错。

    int ** Array=new int*[N];
    for(int i=0; i<N; i++)
    {
        Array[i]=new int[N];
    }
    memset(Array, 0, sizeof(Array));

The memset is causing your trouble, and it's wrong for two reasons. memset 给你带来了麻烦,它是错误的有两个原因。 First, let's say you really wanted to do that.首先,假设你真的想这样做。 You don't, but let's say you did.你没有,但假设你有。 How big is sizeof(Array) . sizeof(Array)有多大。 Array is an int ** .数组是一个int ** On a 64-bit machine, that's 8 bytes.在 64 位机器上,这是 8 个字节。 So conveniently, you only destroyed the first pointer.如此方便,您只销毁了第一个指针。

What you really need to do is this:你真正需要做的是:

    int ** Array=new int*[N];
    // Don't do this memset, but showing you what it should look like)
    memset(Array, 0, sizeof(int *) * N);
    for(int i=0; i<N; i++)
    {
        Array[i]=new int[N];
        // But this one is safe
        memset(Array[i], 0, sizeof(int) * N);
    }

Your version was erasing the first pointer -- Array[0] .您的版本正在擦除第一个指针 -- Array[0] I put that first memset in there so you could see what it would look like if you needed it.我把第一个 memset 放在那里,这样你就可以在需要时看到它的样子。 It's the second one you need.这是你需要的第二个。 You're clearing out the size of an int times the number of ints that you have.您正在清除 int 的大小乘以您拥有的 int 的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM