robinhood46
robinhood46

Reputation: 67

C - Solving Magic Square

I am trying to create a program which checks whether a given matrix is a magic square or not. A magic square is where the sum of all the numbers in each of the sequences vertically, horizontally and diagonally are the same. Whatever combination of numbers I enter, it always returns Magic Square as true. Edit: Fixed diagonals (kinda), except it outputs additional info along with the diagonal.

#include <stdio.h>
#include <stdlib.h>
//#include <stdbool.h>

int main() 
{
int nRows;
int nCols;
int sumR;
int sumC;
int sumD = 0;
int sumD2 = 0;
int k;
int p;
//bool isMagic = 1;


//Defining Matrix order
printf("Enter number of Rows: \n");
scanf("%i",&nRows);


printf("Enter number of Columns: \n");
scanf("%i",&nCols);

int matrix[nRows][nCols];
//int SumR[nRows];
//sint SumC[nCols];

//Enterring Coefficients of the Matrix
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
        printf("Enter value for Matrix[%i][%i]: ",k,p);

        scanf("%i",&matrix[k][p]);
    }
}


//Adding diagonal elements
for(k = 0; k < nRows; ++k){
    //for(p = 0; p < nCols; ++p){
        //if(k == p ){
            sumD = sumD + matrix[k][k];
            sumD2 = sumD2 + matrix[k][nCols - k - 1];
            //sumD2 += matrix[k]
        //}
    //}
    printf("Sum of the Diagonals = %i\n",sumD);
    printf("Sum of the Diagonals2 = %i\n",sumD2);
    //sumD2 = 0;
    //sumD = 0;

}


//Adding the rows
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
        sumR += matrix[k][p];
    }
    printf("Sum of the %i row is = %i\n",k,sumR);
    //if(sumR != sumC)
        //isMagic = 0;
    sumR = 0;
}

//Adding the columns
for(p = 0; p < nRows; ++p){
    for(k = 0; k < nCols; ++k){
        sumC += matrix[k][p];
    }
    printf("Sum of the %i column is = %i\n",p,sumC);
    //if(sumC != sumR)
        //isMagic = 0;
    sumC = 0;

}


//Displaying matrix
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
    printf("%i\t",matrix[k][p]);
}
printf("\n");
} 



if(sumR == sumC == sumD == sumD2){
    printf("\nMagic Square!\n");
}
else{
    printf("\nNot a Magic Square!\n");
}


return 0;
//col[n]+row[n] == userinput

}

Upvotes: 0

Views: 1000

Answers (4)

robinhood46
robinhood46

Reputation: 67

Okay so I used the isMagic method to solve this, as suggested by @Javier Galan. Here is the complete, working code.

#include <stdio.h>

int main() 
{
int nRows;
int nCols;
int sumR;
int sumC;
int sumD = 0;
int sumD2 = 0;
int k;
int p;
int isMagic = 1;


//Defining Matrix order
printf("Enter number of Rows: \n");
scanf("%i",&nRows);


printf("Enter number of Columns: \n");
scanf("%i",&nCols);

int matrix[nRows][nCols];

//Enterring Coefficients of the Matrix
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
        printf("Enter value for Matrix[%i][%i]: ",k,p);

        scanf("%i",&matrix[k][p]);
    }
}


//Adding diagonal elements
for(k = 0; k < nRows ; ++k){
        sumD += matrix[k][k];
        sumD2 += matrix[k][nCols - k - 1];
}
    printf("Sum of right Diagonal = %i\n",sumD);
    printf("Sum of left Diagonal = %i\n",sumD2);



//Adding the rows
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
        sumR += matrix[k][p];
    }
    printf("Sum of the %i row is = %i\n",k,sumR);
        if(sumR != sumD && sumD2)
         isMagic = 0;
    sumR = 0;
}

//Adding the columns
for(p = 0; p < nRows; ++p){
    for(k = 0; k < nCols; ++k){
        sumC += matrix[k][p];
    }
    printf("Sum of the %i column is = %i\n",p,sumC);
        if(sumC != sumD && sumD2)
         isMagic = 0;
    sumC = 0;

}


//Displaying matrix
for(k = 0; k < nRows; ++k){
    for(p = 0; p < nCols; ++p){
    printf("%i\t",matrix[k][p]);
}
printf("\n");
}



if(isMagic){
    printf("\nMagic Square!\n");
}
else{
    printf("\nNot a Magic Square!\n");
}


return 0;

}

Upvotes: 0

EkcenierK
EkcenierK

Reputation: 1439

You are setting sumR and sumC to zero at the end of each loop. They both have value zero after the calculations. This is why when you perform your check to see if they're equal, it always returns true.

At the moment that you print the values, they appear correct, but then you set it to zero and you're throwing that value away so you cannot use it again later! You need to store an array of row sums and an array of column sums, so you have one sum for every column and every row. Inrtead of just int sumR, use something likeint sumR [nCols]; and similarly for sumC

Upvotes: 1

Javier Galan
Javier Galan

Reputation: 49

The best way can do this is to assume it is a magic matrix. Definning a new variable

int isMagic = 1;

You obtain the sum of the diagonal. And put it in sumD.

Then, replace sumR = 0; by

if (sumR != sumD ) isMagic = 0;

and sumC = 0; by

if (sumC != sumD ) isMagic = 0;

and magic!

if( isMagic) {
    printf("\nMagic Square!\n");
}
else{
    printf("\nNot a Magic Square!\n");}

Upvotes: 1

ForeverStudent
ForeverStudent

Reputation: 2537

There are multiple issues with your code:

1- you have the value 2 hardcoded in at some point instead of the number of rows and columns.

2- sumC and sumR are set back to 0 at the very end of execution

3- why do you have one SumR and one SumC when you have multiple rows and columns?

if you want to store the sum or each row ans sum of each column you would need SumR[nCols] and SumC[nRows]

Upvotes: 0

Related Questions