Page 445 - Cs_withBlue_J_C11_Flipbook
P. 445

Step 2:   If  number of column of first matrix =             Matrix multiplication
                         number of rows of second matrix then
                         go to Step 3, else go to Step 15.
                 Step 3:  Initialise i to 0.                    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 4:   Repeat Step 5 to Step 13 until i < row_first.
                 Step 5:  Initialise j to 0.

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

                 Step 8:  Initialise k to 0.

                 Step 9:  Repeat Step 10 to Step 11 until k < column_first.
                 Step 10: Multiply first_array[i][k] with second_array[k][j] and accumulate the result in array product[i][j].

                 Step 11: Increase k by 1.
                 Step 12: Increase j by 1.

                 Step 13: Increase i by 1.
                 Step 14: Display the product matrix, go to Step 16.

                 Step 15: Display matrix multiplication is not possible.

                 Step 16: Stop.

                 14.5.6 Algorithms on String
                 Problem 16: Write an algorithm to count how many times a word occurs in a sentence.

                 Step 1:  Start.

                 Step 2:  Accept sentence in variable sen and search word in search_word.
                 Step 3:  Add space at the end of sen.
                 Step 4:  Initialise count to 0 and word to null.

                 Step 5:  Initialise length as sentence length.
                 Step 6:  Initialise i to 0.

                 Step 7:  Repeat Step 8 to Step 13 while i<length.
                 Step 8:  Extract each character in ch.

                 Step 9:  If ch is equal to space then go to Step 10, else go to Step 12.
                 Step 10: If word = search_word then increment the count by 1.

                 Step 11: Initialise the word to null, go to Step 13.
                 Step 12: Add ch to word.
                 Step 13: Increment i by 1.

                 Step 14: Display count.
                 Step 15: Stop.






                                                                                                                       443
                                                                            Implementation of Algorithms to Solve Problems  443
   440   441   442   443   444   445   446   447   448   449   450