#include "IdanLib.h"

#define N 3

unsigned short CountCharInColumn(char mat[][N], unsigned short row_size ,unsigned short column, char c)
{
    unsigned short count = ZERO,
                   counter;
    for (counter = ZERO; counter < row_size; counter++)
    {
        count += (mat[counter][column] == c);
    }

    return (count);
}

unsigned short CountCharInRow(char mat[][N], unsigned short col_size ,unsigned short row, char c)
{
    unsigned short count = ZERO,
                   counter;
    for (counter = ZERO; counter < col_size; counter++)
    {
        count += (mat[row][counter] == c);
    }

    return (count);  
}

unsigned short CountCharInMainDiagonal(char mat[][N], unsigned short size, char c)
{
    unsigned short count = ZERO,
                   counter;
    for (counter = ZERO; counter < size; counter++)
    {
        count += (mat[size][size] == c);
    }

    return (count);  
}

unsigned short CountCharInSecondaryDiagonal(char mat[][N], unsigned short size, char c)
{
    unsigned short count = ZERO,
                   counter;
    for (counter = ZERO; counter < size; counter++)
    {
        count += (mat[size][N - size] == c);
    }

    return (count); 
}

void OutputPrintMatrix(char 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("%c, ", mat[counter_row][counter_col]);
        }
        printf("\n");
    }
}

unsigned short MaxNumberInArray(unsigned short array[], int size)
{
    int max = array[ZERO],
        counter = ONE;
    for (counter = ONE; counter < size; counter++)
    {
        max = (array[counter] > max) ? array[counter] : max;
    }

    return (max);    
}

unsigned short MaxCountNumberInColumn(int mat[][N], unsigned short row_size ,unsigned short column)
{
    unsigned short counter_array[TEN] = { ZERO },
                   counter,
                   max;
    for (counter = ZERO; counter < row_size; counter++)
    {
        counter_array[mat[counter][column]]++;
    }

    max = MaxNumberInArray(counter_array, TEN);
    return (max);
    
}

unsigned short MaxCountNumberInRow(int mat[][N], unsigned short col_size ,unsigned short row)
{
    unsigned short counter_array[TEN] = { ZERO },
                   counter,
                   max;
    for (counter = ZERO; counter < col_size; counter++)
    {
        counter_array[mat[row][counter]]++;
    }

    max = MaxNumberInArray(counter_array, TEN);
    return (max);
    
}

BOOLEAN IsLatinSquare(int mat[][N], unsigned short row_size)
{
    unsigned short counter = ZERO;
    BOOLEAN is_latin_square = TRUE;
    while ((row_size - counter) * is_latin_square)
    {
        is_latin_square *= (MaxCountNumberInColumn(mat, row_size, counter) == ONE) *
                           (MaxCountNumberInRow(mat, row_size, counter) ==  ONE);
        counter++;
    }
    
    return (is_latin_square);
}

void InputMatrix(int mat[][N], unsigned short row_size)
{
    unsigned short counter,
                   area = row_size * row_size;
    for (counter = ZERO; counter < area; counter++)
    {
        scanf(" %d", &mat[counter / row_size][counter % row_size]);
    }
    
}

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);
}