1

我正在编写一个 C++ 程序,它将从文本文件中确定幻方。虽然矩阵的大小打印得很好,但我里面的所有数据都打印为相同的垃圾数据。例子:

Matrix = 3

-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460

This is a 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);

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";
}

有什么明显的妨碍正常打印的吗?我查了很多例子,但似乎没有一个能解决我的具体问题。感谢您的任何指示或建议。

谢谢你的帮助!

4

0 回答 0