Page 444 - Cs_withBlue_J_C11_Flipbook
P. 444

Step 4:  Initialise small to array[i] and pos to i.                                  6   3  7  2   8  1 *

              Step 5:  Initialise j to i + 1.                                                      1   3  7  2 *  8  6

              Step 6:  Repeat Step 7 to Step 10 while j < size.                                    1   2  7  3 *  8  6

              Step 7:  If array[j] < s then go to Step 8, else go to Step 10.                      1   2  3  7   8  6 *

              Step 8:  Assign s = array[j].                                                        1   2  3  6   8  7 *
                                                                                                   1   2  3  6   7  8
              Step 9:  Assign pos = j.
              Step 10: Increment j by 1.

              Step 11: Swap array[i] and array[pos].

              Step 12: Increment i by 1.

              Step 13: Display sorted array.

              Step 14: Stop.

              14.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 to 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 to 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.


                442442  Touchpad Computer Science-XI
   439   440   441   442   443   444   445   446   447   448   449