25-Comp-A4 Program Design and Data Structures · December 2017
Question 3 of 9: Programming — Saddle Point in a 2-D Array
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Notes on this paper
Pseudocode or any high-level language is
accepted, and the examiner's note states explicitly that marking emphasises
the operation of the program, not syntactic details.
Reference texts for this subject.
Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms,
4th ed. — tree traversals and BSTs (ch. 12), recursion and
divide-and-conquer (ch. 2, 4), asymptotic analysis (ch. 3).
Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. —
linked lists and stacks (ch. 3), binary trees (ch. 4).
Deitel & Deitel, C++ How to Program, 10th ed. — class
design and operator overloading (ch. 9–11), file streams (ch. 14).
Kernighan & Ritchie, The C Programming Language, 2nd ed. —
arrays and file I/O (ch. 1, 7), pointers, structures and linked
lists (ch. 5–6).
Stroustrup, The C++ Programming Language, 4th ed. — class
templates and value semantics (ch. 3, 25–27).
The Computer Engineering citation list is built around architecture and
networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is
programming and data structures, so the works above are cited instead.
Question 3: Programming — Saddle Point in a 2-D Array (20 marks)
Given. An $n\times n$ integer array, values in $[1,99]$,
guaranteed to contain exactly one saddle point (row-minimum that is also its
column's maximum). Worked example: the given $6\times6$ array, saddle
element 4 at $A(3,3)$ (1-indexed).
Find. A program that reads the array and prints the
saddle value and its (row, column) location.
Approach. For each row, find its minimum and the column
it occurs in, then check whether that same value is the maximum of that
column; since exactly one saddle point exists, stop as soon as it is found.
Note why a per-row check suffices. The saddle point
must be a row minimum, so scanning every row's minimum and testing it
against its own column's maximum will find it in at most $n$ row scans
(each row scan is $O(n)$ for the minimum plus $O(n)$ to re-scan its column),
i.e. $O(n^2)$ overall — no need to compare every pair of rows and
columns.
Write the program.
#include <stdio.h>
#define MAXN 50
int main(void)
{
int a[MAXN][MAXN];
int n, i, j, min_val, min_col, col_max;
int found = 0;
printf("Enter n: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < n && !found; i++) {
min_val = a[i][0];
min_col = 0;
for (j = 1; j < n; j++)
if (a[i][j] < min_val) { min_val = a[i][j]; min_col = j; }
col_max = a[0][min_col];
for (j = 1; j < n; j++)
if (a[j][min_col] > col_max) col_max = a[j][min_col];
if (min_val == col_max) {
printf("Saddle element: %d at row %d, column %d\n",
min_val, i + 1, min_col + 1);
found = 1;
}
}
return 0;
}
Confirm against the worked example. Row 3 (1-indexed)
is $8\ 9\ 4\ 5\ 6\ 8$, whose minimum is 4 at column 3; column 3 is
$1,2,4,3,1,2$, whose maximum is 4 — the two agree, so the saddle
point is confirmed at $(3,3)$ with no earlier row (1 or 2) satisfying the
test.
$$\boxed{\text{saddle}=4 \text{ at } (row=3,\ col=3)}$$