Program To Test whether or not a 4x4 matrix is a magic square using functions in C++ [closed]

StackOverflow https://stackoverflow.com/questions/21970762

سؤال

Here is what I have. I have it outputting most of the sums just so i can check there values. I think the problem is with the value of the elements in the array storing the column sums. I would greatly appreciate any feedback.

#include <iostream>
#include <cmath>
using namespace std;

void fillMatrix(int matrix[][4],const int SIZE);
int rowSum(int matrix[][4],const int SIZE,int row[]);
int columnSum(int matrix[][4], const int SIZE, int column[]);
bool isMagic(int matrix[][4], const int SIZE,int row[],int column[]);

int main()
{
const int SIZE=4;
int matrix[SIZE][SIZE];
int row[4],column[4];//arrays to be filled with row and column sums.
char response=0;

cout<<"This program determines whether or not a 4x4 square matrix is a magic square.\n";
do
{
    fillMatrix(matrix,SIZE);
    rowSum(matrix,SIZE,row);
    columnSum(matrix,SIZE,row);



    if(isMagic(matrix,SIZE,row,column))
        cout<<"This is a magic square.\n\n";
    else {
        cout<<"This is not a magic square.\n\n";
    }
    cout<<"To end this program, enter q. To check another matrix, enter any other letter.\n";
    cin>>response;
}while(response!='q'&&response!='Q');

return 0;
}

void fillMatrix(int matrix[][4],const int SIZE)
{
for(int i=0;i<4;i++)
{
    cout<<"Enter four values for row "<<i+1<<".\n";
    for(int j=0;j<4;j++)
    {
        cin>>matrix[i][j];
    }
}
}

int rowSum(int matrix[][4],const int SIZE,int row[4])
{ 
int i=0;
int rowsum=0;
for(i=0;i<4;i++)
{
    rowsum=0;
    for(int j=0;j<4;j++)
    {
        rowsum+=matrix[i][j];
    }
   row[i]=rowsum;
    cout<<row[i]<<endl;
}
return row[i];

}
int columnSum(int matrix[][4], const int SIZE, int column[4])
{
int j=0;
int columnsum=0;
for(j=0;j<4;j++)
{
    columnsum=0;
    for(int i=0;i<4;i++)
    {
        columnsum+=matrix[i][j];
    }
    column[j]=columnsum;
    cout<<column[j]<<endl;
}
return column[j];
}

bool isMagic(int matrix[][4], const int SIZE,int row[4],int column[4])
{
int rightdiagonalsum=0, leftdiagonalsum=0, check;

for(int i=0;i<4;i++)
{
    rightdiagonalsum+=matrix[i][i];
}
cout<<rightdiagonalsum<<endl;
for(int i=0;i<4;i++)
{
    leftdiagonalsum+=matrix[i][3-i];
}
cout<<leftdiagonalsum<<endl;
for(int i=1;i<4;i++)
{
    if (row[i]==row[i-1])
    {
        check=row[i];
    }
    else {
        return false;
    }
}
for(int j=0;j<4;j++)
{
    if (column[j]!=check)
    {
        cout<<column[j]<<"*****";//For some reason, the value of column[j] is 0.
        return false;
    }
}

if (rightdiagonalsum!=check||leftdiagonalsum!=check)
{
    return false;
}

return true;

}
هل كانت مفيدة؟

المحلول

This code:

rowSum(matrix,SIZE,row);
columnSum(matrix,SIZE,row);

should be:

rowSum(matrix,SIZE,row);
columnSum(matrix,SIZE,column);

So the column array in your code has zero values for the rather mundane reason that you never initialised it.

What's more you are accessing beyond the end of the arrays here:

return row[i];

and here:

return column[j];

At both of these points i and j have values 4. You would have avoided such a mistake had you declared the loop variables inside the for statement. That would have limited the scope of these variables.

To fix the problem, return rowsum and columnsum respectively.

I do wonder what purpose the various SIZE declarations serve since your hard code a value of 4 all over the place.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top