PHP Program to insert values into a matrix

How to insert values into a matrix?

To insert values into a matrix first we have to read the values into the rows and columns of the matrix. And by using the for loop we have to read values into each position of row and column. For example, if the number of rows is 3 and the number of columns is 3 we have to read the values into "(0,0) (0,1) (0,2)(1,0) (1,1) (1,2) (2,0) (2,1) (2,2)"

How to insert values into a matrix using PHP?

To insert the values into the matrix first we have to create an empty array a[]. Then we have to accept the values into the variables row and col from the user to store the number of rows and columns.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 accept the values into the array[i][j] from the user and after the completion of the loop we can print the values in the array a[] using for loop.

ALGORITHM

Step 1: Initialize an empty array a[]

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

Step 3: 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) Accept the value into the array a[i][j] from the user

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

PHP Source Code

                                          <?php
$a = array();
$flag = true;
$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";
}
?>
                                      

OUTPUT

Enter the number of rows:  3
Enter the number of columns:  3
Enter the value at position 0 0 :  5
Enter the value at position 0 1 :  6
Enter the value at position 0 2 :  7
Enter the value at position 1 0 :  8
Enter the value at position 1 1 :  4
Enter the value at position 1 2 :  1
Enter the value at position 2 0 :  9
Enter the value at position 2 1 :  3
Enter the value at position 2 2 :  2
The matrix:
5 6 7
8 4 1
9 3 2