dimanche 6 décembre 2015

C++ Magic Square from txt: Only reads first square

This is related to another question I've asked, but now I've gotten a little farther. I am trying to create a Magic Square program based on a text file input. The program will read the first square but then prints out gibberish for the rest of them. Where there is an individual number, it's supposed to set the parameters for the square following it. So here's an example from the text file:

3      <- this indicates the rows&columns
4 9 2  <- row 1
3 5 7  <- row 2
8 1 6  <- row 3
5      <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20

Here's the program that I have so far. I'm trying to follow the assignment instructions, which is why I have 2d arrays and functions set up the way I do

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int SIZE = 20;

void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);

int main()
{
    int n;
    int square[SIZE][SIZE];
    ifstream inputFile;
    inputFile.open("Prog2Input.txt");

    while (inputFile >> n)
    {
        readSquare(n, square);
        printSquare(n, square);
        if (checkMagic(n, square))
        {
            cout << "Magic Square" << endl;
        }
        else
        {
            cout << "NOT Magic Square" << endl;
        }
        system("pause");
    }

    system("pause");
    return 0;
}

void readSquare(int n, int square[][SIZE]) {
    ifstream inf("Prog2Input.txt");
    inf >> n;
    int x;
    for (int i = 0; i<n; i++) {
        for (int j = 0; j<n; j++) {
            inf >> x;
            square[i][j] = x;
        }
    }
}

void printSquare(int n, int square[][SIZE]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << square[i][j] << " ";
        }
        cout << endl;
    }
}
bool checkMagic(int n, int square[][SIZE]) {
    int total = ((1 + n*n) / 2)*n;
    for (int i = 0; i < n; i++) {
        if (sumRow(i, n, square) != total) {
            return false;
        }
    }
    for (int i = 0; i<n; i++) {
        if (sumColumn(i, n, square) != total) {
            return false;
        }
    }
    if (sumDiagonal1(n, square) != total) {
        return false;
    }
    if (sumDiagonal2(n, square) != total) {
        return false;
    }
    return true;
}

int sumRow(int row, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[row][i];
    }
    return sum;
}

int sumColumn(int col, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][col];
    }
    return sum;
}

int sumDiagonal1(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][i];
    }
    return sum;
}

int sumDiagonal2(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][(n - i) - 1];
    }
    return sum;
}

Aucun commentaire:

Enregistrer un commentaire