#include "IdanLib.h"
#include <stdio.h>

#define N 3

int ColumnSum(int mat[][N], unsigned short row_size ,unsigned short column)
{
    unsigned short counter;
    int sum = ZERO;
    for (counter = ZERO; counter < row_size; counter++)
    {
        sum += mat[counter][column];
    }

    return (sum);
    
}

int RowSum(int mat[][N], unsigned short col_size ,unsigned short row)
{
    unsigned short counter;
    int sum = ZERO;
    for (counter = ZERO; counter < col_size; counter++)
    {
        sum += mat[row][counter];
    }

    return (sum);
}

int MainDiagonalSum(char mat[][N], unsigned short col_size)
{
    unsigned short counter;
    int sum = ZERO;
    for (counter = ZERO; counter < col_size; counter++)
    {
        sum += mat[counter][counter];
    }

    return (sum); 
}

int SecondaryDiagonalSum(int mat[][N], unsigned short col_size)
{
    unsigned short counter;
    int sum = ZERO;
    for (counter = ZERO; counter < col_size; counter++)
    {
        sum += mat[counter][col_size - ONE - counter];
    }

    return (sum);
}

int InputInt()
{
    int num;
    scanf(" %d", &num);

    return (num);
}

BOOLEAN IsMagicSqaure(int mat[][N], unsigned short row_size)
{
    int sum = MainDiagonalSum(mat, row_size);
    BOOLEAN is_magic_sqaure = (sum == SecondaryDiagonalSum(mat, row_size));
    unsigned short counter = ZERO;

    while ((row_size - counter) * is_magic_sqaure)
    {
        is_magic_sqaure *= (ColumnSum(mat, row_size, counter) == sum) *
                           (RowSum(mat, row_size, counter) ==  sum);
        counter++;
    }
    
    return (is_magic_sqaure);
}

void InputMatrix(int mat[][N], unsigned short row_size)
{
    unsigned short counter,
                   area = row_size * row_size;
    for (counter = ZERO; counter < area; counter++)
    {
        mat[counter / row_size][counter % row_size] = InputInt();
    }
}
  
void OutputPrintRequestForMatrix()
{
    printf("Enter 9 numbers for matrix: ");
}

void OutputPrintMagicSquare(BOOLEAN is_magic_square)
{
    printf("This is magic square: %hu\n", is_magic_square);
}

void OutputPrintMatrix(int mat[][N], unsigned int row_size, unsigned int column_size)
{
    unsigned int counter_row,
                 counter_col;
    for (counter_row = ZERO; counter_row < row_size; counter_row++)
    {
        for (counter_col = ZERO; counter_col < column_size; counter_col++)
        {
            printf("%d, ", mat[counter_row][counter_col]);
        }
        printf("\n");
    }
}

void main(void)
{
    int mat[N][N] = { ZERO };
    OutputPrintRequestForMatrix();
    InputMatrix(mat, N);
    OutputPrintMatrix(mat, N, N);
    OutputPrintMagicSquare(IsMagicSqaure(mat, N));
}