For the given question we are given an nxn 2D array:
We're trying to fing the max element in the given code. Let's analyze each part A, B, C and D part by part.
Part A: max := ?
We need to set a value for Max, such that the value will be compared to each and every element in the array and if the current value is larger than the Max value then we'd update the current value to be the max value, in this way we shall get the Maximum value of the array after traversing the whole array.
1)We can set the max value to be the first value of the array as it would be compared to all the element in the array, i.e.,
A[0][0]
[ WRONG: Setting the max value to be 0. Because if we are given an array with all negative elements then we'd get wrong answer. ]
Part B, C and D :
while |B| do
begin
if A[i, j]>max then max:=A[i, j];
if |C| then j:=j+1;
else begin
j:=0;
i:=|D|
inside the while loop we are comparing the current and max value as discussed earlier. By traversing 'i' we are skipping columns and traversing in the row and by traversing 'j' we are skipping rows and traversing in the coulmns(NOTE: In the row-major ordering we wont directly rows unless columns are over.)
Part B would be the condition checking for traversing by the rows, hence i<n
Part C would be the condidtion to check for gooing to the next element until j<n
Part D is the else condition when j exceeds n-1, there we would set j=0 and then go to the next row by i = i+1