提供垃圾数据的幻方数组 我正在编写一个C++程序,它将从文本文件中确定幻方。虽然矩阵的大小打印得很好,但我的所有数据都作为相同的垃圾数据打印。例如: Matrix = 3 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 This is a magic square!

提供垃圾数据的幻方数组 我正在编写一个C++程序,它将从文本文件中确定幻方。虽然矩阵的大小打印得很好,但我的所有数据都作为相同的垃圾数据打印。例如: Matrix = 3 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 This is a magic square!,c++,arrays,magic-square,C++,Arrays,Magic Square,代码如下: #include <iostream> #include <fstream> #include <string> using namespace std; int readSquare(int matrix1[][40]); void printMatrix(int matrix1[][40], int matrixSize1); void checkValid(int matrix1[][40], int matrixSize1); in

代码如下:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int readSquare(int matrix1[][40]);
void printMatrix(int matrix1[][40], int matrixSize1);
void  checkValid(int matrix1[][40], int matrixSize1);

int main()
{
    int matrix1[40][40];
    //call function to open file and load matrix array with matrix found in file
    int matrixSize = readSquare(matrix1);

    //call function to output
    if (matrixSize)
    {
        printMatrix(matrix1, matrixSize);

        //call function to verify if square is a magic square
        checkValid(matrix1, matrixSize);
    }
    else
        cout << "Unable to continue." << endl << endl;


    system("pause");
    return 0;
}

int readSquare(int matrix1[][40])
{
    ifstream input;
    //open file
    input.open("Prog2Input.txt");
    int matrix[1600];
    int test[1600];
    //create object to read file




    //counter
    int rows = 1, num, size = 1;
    while (input >> num)
    {
        if (test[num] == 0)
        {
            matrix[rows] = num;
            test[num] = num;
            rows++;
            if (size < num)
                size = num;
        }
        else
            cout << "The Magic Square had an error." << endl;
    }
    rows--;
    num = 3;
    while (num*num < size)
        num++;


    //load matrix in array
    int z = 1;
    for (int x = 1; x <= num; x++)
    {
        for (int y = 1; y <= num; y++)
        {
            matrix1[x][y] = matrix[z];
            z++;
        }
    }
    //close file
    input.close();

    return num;


}

void printMatrix(int matrix1[][40], int matrixSize1)
{
    cout << "Matrix\n\n";
    cout << "Matrix Size = " << matrixSize1 << endl << endl;

    // Dislay Matrix
    for (int x = 1; x <= matrixSize1; x++)
    {
        for (int y = 1; y <= matrixSize1; y++)
        {
            if (matrix1[x][y] < 10)
                cout << " ";
            cout << matrix1[x][y] << " ";
        }
        cout << endl;
    }
}

void checkValid(int matrix1[][40], int matrixSize1)
{
    int Row_Total = 0, Total;
    bool Square = true;
    // Get # for top r
    for (int x = 1; x <= matrixSize1; x++)
        Row_Total += matrix1[1][x];

    // Read  row compare to top
    for (int x = 1; x <= matrixSize1; x++)
    {
        Total = 0; // Reset variable for next row
        for (int y = 1; y <= matrixSize1; y++)
        {
            Total += matrix1[x][y];
        }
        if (Total != Row_Total)
            Square = false;
    }

    // Read  colum compare to row
    for (int y = 1; y <= matrixSize1; y++)
    {
        Total = 0; // Reset # for next column
        for (int x = 1; x <= matrixSize1; x++)
        {
            Total += matrix1[x][y];
        }
        if (Total != Row_Total)
            Square = false;
    }

    Total = 0; // Reset #

               // Read diagonal top left to low right
    for (int x = 1; x <= matrixSize1; x++)
    {
        Total += matrix1[x][x];
    }
    if (Total != Row_Total)
        Square = false;


    // Read diagonal top right to bottom left

    int y = 1;
    Total = 0; // Reset variable
    for (int x = matrixSize1; x >0; x--)
    {
        Total += matrix1[y][x];
        y++;
    }
    if (Total != Row_Total)
        Square = false;


    if (Square)
    cout << "\n" << "Magic Square!" << "\n\n";

    else
    cout << "\n" << "Not A Magic Square." << "\n\n";
}
#包括
#包括
#包括
使用名称空间std;
int-readSquare(int-matrix1[][40]);
无效打印矩阵(int-matrix1[][40],int-matrixSize1);
无效检查有效(int matrix1[][40],int matrixSize1);
int main()
{
int matrix1[40][40];
//调用函数打开文件并加载在文件中找到矩阵的矩阵数组
int matrixSize=readSquare(matrix1);
//调用函数进行输出
if(矩阵大小)
{
printMatrix(matrix1,matrixSize);
//调用函数验证正方形是否为幻方
checkValid(matrix1,matrixSize);
}
其他的

我可以建议您使用
std::array
std::vector
吗?这是在良好的调试器上检查的边界,可能会检测到您遗漏的任何错误。如果您使用vectors,您还可以在运行时指定大小,而不将程序限制为任意大小的正方形。您的测试数组未初始化,将填充随机数据。UNRLaTe:如果你简单地接受C++和C中的数组是零基索引的事实,你将节省大量的头抓伤。我想最可能的原因是你无法打开你的文件。添加下面的代码,替换<代码>输入。打开(“PrP2输入。txt”);< /C> > <代码>如果(输入)打开(Prop2Posith.txt)。))难道我不应该将我的测试初始化为零吗?我建议你使用
std::array
std::vector
,这是在好的调试器上检查的边界,可能会检测到你遗漏的任何错误。如果你使用vector,你也可以在运行时指定大小,而不是将你的程序限制为任意大小的正方形。你的测试数组不是初始化的如果你简单地接受C++和C中的数组是零基索引的事实,我想说最有可能的原因是你无法打开你的文件。添加下面的代码,替换<代码>输入。如果(!input.open(“Prog2Input.txt”))不能将测试初始化为零,那么我应该将测试初始化为什么?