2

My program needs to check if the input matrix is a magic square.

It is almost done, but I have a problem with fuction. I am not sure why but it return wrong values. I need function to return 1 if square is magic and to return 0 if square isn´t magic. I am getting weird output and segmentation fault.

example of input:

3 - size of square, square: 
8 1 6
3 5 7
4 9 2

example of output (function return 1 because square is magic) printed on screen is Square is magic.

Code:

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

int is_magic(int m[][size], size_t size)
{
    int sum = 0;

    for(size_t col = 0; col < size; col++)
    {
        sum += m[col];
    }

    for(size_t row = 1; row < size; row++)
    {
        int psum = 0;
        for(size_t col = 0; col < size; col++)
        {
            psum += m[col + row * size];
        }
        if(psum != sum) return 0;
    }

    for(size_t col = 0; col < size; col++)
    {
        int psum = 0;
        for(size_t row = 0; row < size; row++)
        {
            psum += m[col + row * size];
        }
        if(psum != sum) return 0;
    }
    return 1;
}


int main()
{
      int size,row,column;
      scanf("%d",&size);
      int *matrix
      matrix = (int**)malloc(size * sizeof(int*));
            for (row = 0; row < size; row++)
                  matrix[row] = (int*)malloc(size * sizeof(int));


      for (row = 0; row < size; row++)
             for (column = 0; column < size; column++)
                   scanf("%d ", &matrix[row][column]);


       if(is_magic(matrix, size))
      {
            printf("Magic square");
      }
      else
      {
            printf("Not magic square");
      }
    return 0;
}
Dudo
  • 27
  • 5
  • 1
    `int matrix[size][size]` is not `int ** matrix`. – vgru Dec 22 '18 at 10:48
  • Possible duplicate of [How to pass 2D array (matrix) in a function in C?](https://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c) – vgru Dec 22 '18 at 10:49
  • First you need to explain what the magic square is – 0___________ Dec 22 '18 at 10:51
  • @P__J__: sum of elements in each row, column or diagonal must be equal (15 in this case). It doesn't seem it will work, but at least passing the matrix correctly will allow OP to debug. – vgru Dec 22 '18 at 10:56
  • @Groo The answer may be a dupe, but not the question – John Dec 22 '18 at 10:57
  • @P__J__ I think that's irrelevant as the author gets set faults and his magic number calculation logic is up to them – John Dec 22 '18 at 10:58
  • @John: the problem OP is having is because you cannot pass a 2d array into a function this way. – vgru Dec 22 '18 at 10:59
  • @Groo I've provided an answer to that already – John Dec 22 '18 at 11:00

4 Answers4

1

The problem is with the signature of the is_magic function. You need to pass in a two dimensional array to it;

int is_magic(int **matrix,int size);

Here you are passing a pointer to an int pointer.

What you need is to pass a two dimensional array as follows:

int is_magic(int matrix[][size], int size);

Make sure to use a C99 compliant compiler.

Quick side note, you may or not need to reverse the order of the arguments for the is_magic function.

You actually need to reverse the order of the parameters as follows to comply with the function call order of arguments.

int is_magic(int size, int matrix[][size]);
John
  • 1,012
  • 14
  • 22
0

Two dimensional array is just the continuous chink of memory. If the array has same number of columns and rows the order of storing them is not important in this case.

int is_magic(int *m, size_t size)
{
    int sum = 0;

    for(size_t col = 0; col < size; col++)
    {
        sum += m[col];
    }

    for(size_t row = 1; row < size; row++)
    {
        int psum = 0;
        for(size_t col = 0; col < size; col++)
        {
            psum += m[col + row * size];
        }
        if(psum != sum) return 0;
    }

    for(size_t col = 0; col < size; col++)
    {
        int psum = 0;
        for(size_t row = 0; row < size; row++)
        {
            psum += m[col + row * size];
        }
        if(psum != sum) return 0;
    }
    return 1;
}

int m[3][3] = 
{
    {8, 1, 6},
    {3, 5, 7},
    {4, 9, 2},
};

int m1[3][3] = 
{
    {8, 1, 6},
    {3, 9, 7},
    {4, 9, 2},
};


int main()
{
    printf("%d\n", is_magic(m,3));
    printf("%d\n", is_magic(m1,3));

    return 0;
}

Working example: https://ideone.com/P4C3H2

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Are you sure row should start from 1 in here `for(size_t row = 1; row < size; row++)` ? – John Dec 22 '18 at 11:10
  • 1
    yes ,I do not need to calculate the first one (the one with index 0) as it was already done in the previous loop. – 0___________ Dec 22 '18 at 11:11
  • You misspelled chunk in the answer (: – John Dec 22 '18 at 11:17
  • I updated code but now I am getting compilation error, i think it has something to do with allocating memory for 2d array doesn´t it? – Dudo Dec 22 '18 at 11:23
  • @Dudo what compilation problem? – 0___________ Dec 22 '18 at 11:24
  • error: ‘size’ undeclared here (not in a function); did you mean ‘size_t’? int is_magic(int m[][size], size_t size) ^~~~ size_t In function ‘main’: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘matrix’ matrix = (int**)malloc(size * sizeof(int*)); ^~~~~~ error: ‘matrix’ undeclared (first use in this function); did you mean ‘main’? matrix = (int**)malloc(size * sizeof(int*)); ^~~~~~ main – Dudo Dec 22 '18 at 12:02
  • note: each undeclared identifier is reported only once for each function it appears in error: type of formal parameter 1 is incomplete if(is_magic(matrix, size)) ^~~~~~ – Dudo Dec 22 '18 at 12:02
  • I´ve replaced some lines but I do still get error and in Bigger test with more numbers than just 3 I get that function returned 0 even when square was magic – Dudo Dec 22 '18 at 12:13
  • Do not change my function declaration. You are amending it - but you do not know what you are doing. Leave it as it is. `int is_magic(int *m, size_t size)` not anything else. Do not mix with another solutions. – 0___________ Dec 22 '18 at 12:22
-2

The call of is_magicshould be

flag = is_magic(matrix,size);

as this is consistent with your function definition.

Thomas Z.
  • 17
  • 3
-3

Try the following :

#include<stdio.h>

int is_magic(int **matrix,int size)
{
int sum, sum1, sum2;
int row, column = 0;
//For diagonal elements
sum = 0;
for (row = 0; row < size; row++)
{
    for (column = 0; column < size; column++)
    {
        if (row == column)
            sum = sum + matrix[row][column];
    }
}

//For Rows
for (row = 0; row < size; row++)
{
    sum1 = 0;
    for (column = 0; column < size; column++)
    {
        sum1 = sum1 + matrix[row][column];
    }
    if (sum == sum1)
        return 1;
    else
    {
        return 0;
        break;
    }
}

//For Columns
for (row = 0; row < size; row++)
{
    sum2 = 0;
    for (column = 0; column < size; column++)
    {
        sum2 = sum2 + matrix[column][row];
    }
    if (sum == sum2)
        return 1;
    else
    {
        return 0;
        break;
    }
}
}


int main()
{
int size;
scanf("%d",&size);
int matrix[size][size];
int row=0, column = 0;
int flag = 0;

for (row = 0; row < size; row++)
{
    for (column = 0; column < size; column++)
        scanf("%d", &matrix[row][column]);
    printf("%d\n", matrix[row][column]);
}

flag = is_magic(size,matrix);
if (flag == 1)
    printf("1");
else
    printf("0");

return 0;
}
Mohd Naved
  • 358
  • 3
  • 12
  • Not working mate, compiles but it has random no´s inside – Dudo Dec 22 '18 at 11:12
  • 1
    "Try this" is not an answer. You should at a minimum explain what you changed in the code and why it solves the problem. – JJJ Dec 22 '18 at 11:58