PHP Program to check whether the matrix is sparse matrix or not

What is a sparse matrix?

A matrix is said sparse matrix when the number of 0's in the matrix will be more than other numbers. For example, 

 0  1  0  

 6  0  0

 7  0  0

In the above example, we can see that there are 6 0's and only 3 non 0's so we can say that it is a sparse matrix.

How to check whether the matrix is a sparse matrix or not using PHP?

In this program to check whether the matrix is sparse or not we are accepting the matrix from the user. First, we have to read the number of rows and columns into the variables row and col the we have to read values into the matrix into the array a[] according to the row and col using the for loop. Then we have to assign value 0 into the variable countZero to count the 0's in the matrix and assign the computed value of 'col * row' into the variable matrix. After that, we have to assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to Check condition 'a[i][j] == 0' if true increment the value of the variable countZero. After the completion of the loop check the condition 'countZero > (matrix / 2)' if true print the current matrix as a sparse matrix otherwise print the current matrix as not a sparse matrix

ALGORITHM

Step 1: Initialize an empty array a[]

Step 2: Accept the values into the variables row and col from the user to store the number of rows and columns

Step 3: Accept the values into the array a[] using for loop

Step 4: Print the elements in the array a[] using for loop

Step 5: Create a variable countZero with value 0 to count the 0's in the matrix

Step 6: Assign the computed value of 'col * row' into the variable matrix

Step 7: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < row' becomes false and increment the value of variable i in every iteration

        (i) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j in every iteration

            (a) Check condition 'a[i][j] == 0' if true increment the value of the variable countZero

Step 8: Check the condition 'countZero > (matrix / 2)' if true print the current matrix as sparse matrix otherwise print the current matrix as not a sparse matrix

PHP Source Code

                                          <?php
$a = array();
$row = readline("Enter the number of rows: \n");
$col = readline("Enter the number of columns: \n");
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        $a[$i][$j] = readline("Enter the value at position $i $j :  ");
    }
}
echo "The entered matrix is matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a[$i][$j] . " ";
    }
    echo "\n";
}
$countZero = 0;
$matrix = $col * $row;
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        if ($a[$i][$j] == 0) {
            $countZero++;
        }
    }
}
if ($countZero > ($matrix / 2)) {
    echo "The current matrix is a sparse matrix";
} else {
    echo "The current matrix is not a sparse matrix";
}
?>
                                      

OUTPUT

Example 1
Enter the number of rows:  3
Enter the number of columns:  3
Enter the value at position 0 0 :  0
Enter the value at position 0 1 :  0
Enter the value at position 0 2 :  0
Enter the value at position 1 0 :  0
Enter the value at position 1 1 :  8
Enter the value at position 1 2 :  5
Enter the value at position 2 0 :  6
Enter the value at position 2 1 :  0
Enter the value at position 2 2 :  8
The entered matrix is matrix:
0 0 0
0 8 5
6 0 8
The current matrix is a sparse matrix

Example 2
Enter the number of rows:  3
Enter the number of columns:  3
Enter the value at position 0 0 :  1
Enter the value at position 0 1 :  0
Enter the value at position 0 2 :  2
Enter the value at position 1 0 :  0
Enter the value at position 1 1 :  5
Enter the value at position 1 2 :  4
Enter the value at position 2 0 :  8
Enter the value at position 2 1 :  0
Enter the value at position 2 2 :  0
The entered matrix is matrix:
1 0 2
0 5 4
8 0 0
The current matrix is not a sparse matrix