Problem 2.8:
Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should be added twice.
Solution:
>>First, we have to identify the boundary elements.
- In a two dimensional array, elements of first column and the last column and the first row and last row are the boundary elements as shown in the Figure-2.8.
- When i is 1, the row is the first row and when i is m (where m represents the number of rows in the array), the row is the last row.
- Similarly, when j = 1, the column is the first column and the index of the last column is j = n. So, a number in a two-dimensional array is a boundary element if i = 1, i = m, j = 1 or j = n
The summation Of the boundary elements Of the array
- We shall start from the first number of the array.
- If it is a boundary element, the number will be added to the sum (which is a variable to store the result and initially it is set zero).
- We shall check every number whether it is a boundary element or not, if the number is a boundary element it will be added to sum.
- Otherwise, we shall proceed with the next number of the list (array) and continue the process to the end of the list.