For my c++ class i have to write a code that will read integers in to a 2D array from a file and then, using functions/methods, determine if the array is a magic square. My problem is that when i build or compile i get "error C2664: 'DataIn' : cannot convert parameter 1 from 'std::ifstream' to 'std::fstream &'" Here is the code:

#include <iostream>

#include <fstream>

using namespace std;

void PrintMatrix( int x[][20], int size)
{
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            cout << x[i][j] << "\t";
        }

        cout << endl;
    }
}

int SumRtDiag(int x[][20], int size)
{
    int d = 0;

    for(int i = 0; i < size; ++i)
    {
        d += x[i][i];

        return d;
    }
}

void DataIn(fstream &fin, int x[][20], int size)
{
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            fin >> x[i][j];
        }
    }
}

int SumCol(int x[][20], int size, int NumCol)
{
    int ColSum = 0;

    for(int i = 0; i < size; ++i)
    {
        ColSum += x[i][NumCol];
    }

    return ColSum;
}

int SumRow(int x[][20], int size, int NumRow)
{
    int RowSum = 0;

    for(int j = 0; j < size; ++j)
    {
        RowSum += x[NumRow][j];
    }

    return RowSum;
}

int FindSize(int x[][20], ifstream fin)
{
    int m;

    fin >> m;

    return m;
}

int SumLftDiag(int x[][20], int size)
{
    int sum = 0;

    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            j = size - (i + 1);

            sum = x[i][j];
        }
    }

    return sum;
}

bool IsMagic(int x[][20], int size)
{
    int total;

    total = SumRtDiag(x , size);

    if(total != SumLftDiag(x, size))
    {
        for(int i = 0; i < size; ++i)
        {
            if(SumCol(x, size, i) != total)

                return false;
        }

        for(int j = 0; j < size; ++j)
        {
            if(SumRow(x, size, j) != total)

                return false;
        }

    }

        return false;

        return true;
}

int main()
{
    ifstream fin("Lab 11.txt");

    fin.open("Lab 11.txt");

    if(!fin.is_open())
    {
        cout << "Input error." << endl;

        return 0;
    }

    int x[20][20], size;

    size = FindSize(x, fin);

    DataIn(fin, x, size);

    PrintMatrix(x, size);

    for(int i = 0; i < size; ++i)
    {
        cout << "The sum of row " << i << " equals " << SumRow(x, size, i) << endl;
    }

    for(int j = 0; j < size; ++j)
    {
        cout << "The sum of column " << j << " equals " << SumCol(x, size, j) << endl;
    }

    cout << "The sum of the right diagonal is " << SumRtDiag(x, size) << endl;

    cout << "The sum of the left diagonal is " << SumLftDiag(x, size) << endl;

    if(IsMagic)

        cout << "This array is a magic square." << endl;

    else 

        cout << "This array is not a magic square." << endl;

    system("pause");

    return 0;

}

Any help would be appreciated!

Recommended Answers

All 4 Replies

Errors fixed.
Now work on your magic function

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

void PrintMatrix( int x[][20], int size)
{
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            cout << x[i][j] << "\t";
        }
        cout << endl;
    }
}
int SumRtDiag(int x[][20], int size)
{
    int d = 0;
    for(int i = 0; i < size; ++i)
    {
        d += x[i][i];

    }
    return d;
}
void DataIn(ifstream &fin, int x[][20], int size){


    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            fin >> x[i][j];
        }
    }
}
int SumCol(int x[][20], int size, int NumCol)
{
    int ColSum = 0;
    for(int i = 0; i < size; ++i)
    {
        ColSum += x[i][NumCol];
    }
    return ColSum;
}
int SumRow(int x[][20], int size, int NumRow)
{
    int RowSum = 0;
    for(int j = 0; j < size; ++j)
    {
        RowSum += x[NumRow][j];
    }
    return RowSum;
}
int FindSize(int x[][20], ifstream &fin)
{
    int m;
    fin >> m;
    return m;
}
int SumLftDiag(int x[][20], int size)
{
    int sum = 0;
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            j = size - (i + 1);
            sum = x[i][j];
        }
    }
    return sum;
}
bool IsMagic(int x[][20], int size)
{
    int total;
    total = SumRtDiag(x , size);
    if(total != SumLftDiag(x, size))
    {
        for(int i = 0; i < size; ++i)
        {
            if(SumCol(x, size, i) != total)
                return false;
        }
        for(int j = 0; j < size; ++j)
        {
            if(SumRow(x, size, j) != total)
                return false;
        }
    }
        return false;
        return true;
}
int main()
{
    ifstream fin("Lab11.txt");
   // fin.open("Lab11.txt");  // no need
    if(!fin.is_open())
    {
        cout << "Input error." << endl;
        return 0;
    }
    int x[20][20], size;
    size = FindSize(x, fin);
    DataIn(fin, x, size);
    PrintMatrix(x, size);
    for(int i = 0; i < size; ++i)
    {
        cout << "The sum of row " << i << " equals " << SumRow(x, size, i) << endl;
    }
    for(int j = 0; j < size; ++j)
    {
        cout << "The sum of column " << j << " equals " << SumCol(x, size, j) << endl;
    }
    cout << "The sum of the right diagonal is " << SumRtDiag(x, size) << endl;
    cout << "The sum of the left diagonal is " << SumLftDiag(x, size) << endl;
    if(IsMagic)  // work on your magic logic
        cout << "This array is a magic square." << endl;
    else 
        cout << "This array is not a magic square." << endl;
    system("pause");
    return 0;
}

Thanks for the help!

Ok now i am completely lost. The only change i made other than the one recommended was I plugged in a do-while loop so that the program will run until it reads in a size of -1. Now the program runs until it prints the SumRtDiag then just stops. Any suggestions?

Here is the code again:

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

void PrintMatrix( int x[][20], int size)
{
    cout << "The current matrix is: " << endl;

    for(int i = 0; i < size; ++i)
    {
        cout << "\t";

        for(int j = 0; j < size; ++j)
        {
            cout << x[i][j] << "\t";
        }

        cout << endl;
    }

    cout << endl;
}

int SumRtDiag(int x[][20], int size)
{
    int d = 0;

    for(int i = 0; i < size; ++i)
    {
        d += x[i][i];
    }

    return d;
}

void DataIn(ifstream &fin, int x[][20], int size)
{
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            fin >> x[i][j];
        }
    }
}

int SumCol(int x[][20], int size, int NumCol)
{
    int ColSum = 0;

    for(int i = 0; i < size; ++i)
    {
        ColSum += x[i][NumCol];
    }

    return ColSum;
}
int SumRow(int x[][20], int size, int NumRow)
{
    int RowSum = 0;

    for(int j = 0; j < size; ++j)
    {
        RowSum += x[NumRow][j];
    }

    return RowSum;
}
int FindSize(int x[][20], ifstream &fin)
{
    int m;

    fin >> m;

    return m;
}

int SumLftDiag(int x[][20], int size)
{
    int sum = 0;

    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size-1; ++j)
        {
            j = (size - (i + 1));

            sum += x[i][j];
        }
    }

    return sum;
}

bool IsMagic(int x[][20], int size)
{
    int total;

    total = SumRtDiag(x , size);

    if(total != SumLftDiag(x, size))
    {
        return false;
    }

    for(int i = 0; i < size; ++i)
        {
            if(SumCol(x, size, i) != total)

                return false;
        }

    for(int j = 0; j < size; ++j)
        {
            if(SumRow(x, size, j) != total)

                return false;
        }   

    return true;
}

int main()
{
    int size = 0;

    do
    {
        ifstream fin("C:\\Users\\meekscj\\Documents\\Lab 11.txt");

        if(!fin.is_open())
        {
            cout << "Input error." << endl;

            return 0;
        }

        int x[20][20];

        size = FindSize(x, fin);

        DataIn(fin, x, size);

        PrintMatrix(x, size);

        for(int i = 0; i < size; ++i)
        {
            cout << "The sum of row " << i << " equals " << SumRow(x, size, i) << endl << endl;
        }

        for(int j = 0; j < size; ++j)
        {
            cout << "The sum of column " << j << " equals " << SumCol(x, size, j) << endl << endl;
        }

        cout << "The sum of the right diagonal is " << SumRtDiag(x, size) << endl << endl;

        cout << "The sum of the left diagonal is " << SumLftDiag(x, size) << endl << endl;

        if(IsMagic(x, size))
        {
            cout << "This array is a magic square." << endl;
        }

        else 
        {
            cout << "This array is not a magic square." << endl;
        }

    }while(size != -1);

    system("pause");

    return 0;
}

can you tell us what each function is suppose to do in your code. comments please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.