Page 113 - Computer science 868 Class 12
P. 113

3.5.5 Algorithms on Matrix

                 Problem 14: Write an algorithm to calculate the sum of diagonals of a square matrix  of a given size.


                        Note:  The program already covered in the Array chapter of this book.


                 Step 1:  Start.

                 Step 2:  Initialise diagonal_one and diagonal_two to 0.
                 Step 3:  Initialise i to 0.
                 Step 4:   Repeat Step 5 to Step 12 while i < size.
                 Step 5:  Initialise j to 0.

                 Step 6:  Repeat Step 7 to Step 11 while j < size.
                 Step 7:  If i = j then go to Step 8, else go to Step 9.
                 Step 8:  Add array[i][j] to diagonal_one and store the result in diagonal_one.

                 Step 9:  If  i + j = size - 1  then go to Step 10 else go to Step 11.
                 Step 10: Add array[i][j] to diagonal_two and store the result in diagonal_two.
                 Step 11: Increment j by 1.
                 Step 12: Increment i by 1.

                 Step 13: Display diagonal_one and diagonal_two.
                 Step 14: Stop.

                 Problem 15: Write an algorithm to multiply two matrices and display the product matrix.


                        Note:  The program already covered in the Array chapter of this book.


                 Step 1:  Start.
                 Step 2:  If column_first = row_second then go to Step 3, else go to Step 15.


                                                        Matrix multiplication



                                          1   2  ×   5   6  =   1*5 + 2*0     1*6 + 2*7  =  5   20

                                          3   4      0   7      3*5 + 4*0    3*6 + 4*7   15  46


                 Step 3:  Initialise i to 0.

                 Step 4:  Repeat Step 5 to Step 13 while i < row_first.
                 Step 5:  Initialise j to 0.

                 Step 6:  Repeat Step 7 to Step 12 while i < column_second.
                 Step 7:  Initialise product[i][j] to 0.

                 Step 8:  Initialise k to 0.




                                                                                                                       111
                                                                            Implementation of Algorithms to Solve Problems  111
   108   109   110   111   112   113   114   115   116   117   118