Matrix is markov or not.

C program to check whether matrix is markov or not.

A matrix M is a Markov matrix if and only if its sum of each row is equal to only 1.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 10

void read_matrix(double mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("Element [%d][%d]\t:",i,j);
            scanf("%lf",&mat[i][j]);
        }
    }
}

void print_matrix(double mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%lf\t",mat[i][j]);
        }
        printf("\n");
    }
}

int markov(double mat[MAX][MAX], int row)
{
    int i, j;
    double sum;

    for(i=0;i<row;i++)
    {
        sum = 0;
        for(j=0;j<row;j++)
        {
            sum = sum + mat[i][j];
        }
        if(sum != 1)
        {
            return(0);
        }
    }

    return(1);
}

void main()
{
    double mat[MAX][MAX];
    int row, col;

    printf("Enter the number of rows or columns of the square matrix\t:");
    scanf("%d",&row);
    col = row;

    printf("Enter the elements of the matrix\n");
    read_matrix(mat, row, col);

    printf("\nMatrix entered is\n");
    print_matrix(mat, row, col);

    if(markov(mat, row))
        printf("\nMatrix is a markov matrix");
    else
        printf("\nMatrix is not a markov matrix");

    getch();
}

Output

********** Run1 ********** 

Enter the number of rows or columns of the square matrix        :3
Enter the elements of the matrix
Element [0][0]  :0.9
Element [0][1]  :0.075
Element [0][2]  :0.025
Element [1][0]  :0.15
Element [1][1]  :0.8
Element [1][2]  :0.05
Element [2][0]  :0.25
Element [2][1]  :0.25
Element [2][2]  :0.5

Matrix entered is
0.900000        0.075000        0.025000
0.150000        0.800000        0.050000
0.250000        0.250000        0.500000

Matrix is a markov matrix


********** Run2 ********** 

Enter the number of rows or columns of the square matrix        :3
Enter the elements of the matrix
Element [0][0]  :0.80
Element [0][1]  :0.20
Element [0][2]  :0
Element [1][0]  :0.4
Element [1][1]  :0.5
Element [1][2]  :0.6
Element [2][0]  :0.3
Element [2][1]  :0.6
Element [2][2]  :0.1

Matrix entered is
0.800000        0.200000        0.000000
0.400000        0.500000        0.600000
0.300000        0.600000        0.100000

Matrix is not a markov matrix