recategorized by
5,167 views
26 votes
26 votes

The following Pascal program segments finds the largest number in a two-dimensional integer array $A[0\dots n-1, 0\dots n-1]$ using a single loop. Fill up the boxes to complete the program and write against $\fbox{A}, \fbox{B}, \fbox{C} \text{ and } \fbox{D}$ in your answer book Assume that max is a variable to store the largest value and $i, j$ are the indices to the array.

begin
    max:=|A|, i:=0, j:=0;
    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|
        end
    end
end
recategorized by

3 Answers

Best answer
42 votes
42 votes

We have to traverse all elements in array. The code is doing this row wise. 


begin
    max:=A[0,0], i:=0, j:=0;
    while (i < n) do
    begin
        if A[i, j]>max then max:=A[i, j];
        if (j < n-1) then j:=j+1;
        else begin
            j:=0;
            i:=i++;
        end
    end
end
selected by
3 votes
3 votes
A:=A[0,0]    B:=(i<n)    C:=(j<n-1)   D:=i++
1 votes
1 votes

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

Related questions

19 votes
19 votes
2 answers
1
Kathleen asked Sep 29, 2014
3,032 views
Prove by the principal of mathematical induction that for any binary tree, in which every non-leaf node has $2$-descendants, the number of leaves in the tree is one more ...
32 votes
32 votes
4 answers
4
Kathleen asked Sep 29, 2014
4,808 views
Let $\left(\{ p,q \},*\right)$ be a semigroup where $p*p=q$. Show that:$p*q=q*p$ and$q*q=q$