---
title: "Pass 2D array; largest and smallest"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:pass-2d-array-largest-smallest"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pass-2d-array-largest-smallest
site: BCT Engineering
---

# Pass 2D array; largest and smallest

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2073 Chaitra, 5 marks
- Video: [3. 2D array - pass to function and find MAX and Min](https://www.youtube.com/watch?v=fNgGQGtjXvQ) — Ahmad Darkhalil
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pass-2d-array-largest-smallest
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Explain how 2D array is passed to a function. Write a C program to display largest and smallest elements of a 2D array.

## Short answer

In the function parameter, declare the array with column size, e.g., int a[][10]. Traverse all elements with nested loops, initializing max and min to a[0][0], update them, and print the largest and smallest.

## Answer

#### Passing a 2D array to a function
The array name is written in the call: `findMinMax(a, r, c);`. In the function header the array is declared with all dimensions except the first, for example `int a[][10]`, because the compiler needs the column size to find the address of a[i][j] (address = base + (i × columns + j) × size). The array is passed by address, so no copy is made. The number of rows and columns used are passed as extra arguments.

#### Program

```c
#include <stdio.h>

void findMinMax(int a[][10], int r, int c)
{
    int max = a[0][0], min = a[0][0];
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) {
            if (a[i][j] > max) max = a[i][j];
            if (a[i][j] < min) min = a[i][j];
        }
    printf("Largest = %d\nSmallest = %d\n", max, min);
}

int main(void)
{
    int a[10][10], r, c;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);
    printf("Enter %d elements: ", r * c);
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            scanf("%d", &a[i][j]);
    findMinMax(a, r, c);
    return 0;
}
```

**Sample run**

Input: `2 3 4 9 -2 15 7 3`

```text
Enter rows and columns: Enter 6 elements: Largest = 15
Smallest = -2
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is the 2D array declared in the function header?
   A. With all dimensions except the first, e.g. int a[][10]
   B. With only the first dimension, int a[3][]
   C. Without any dimension
   D. With only the total size
   Answer: A. The column size is required. (Key point: Header declaration)

2. Why does the compiler need the column size?
   A. To check bounds at run time
   B. To allocate the array
   C. To compute the address of a[i][j] = base + (i*columns + j)*size
   D. To print the matrix
   Answer: C. Rows are stored one after another, so the column count fixes where each row starts. (Key point: Why column size)

3. Why are r and c passed to the function?
   A. The function cannot know how much of the array is used
   B. To copy the array
   C. To resize the array
   D. They are the array indices
   Answer: A. The array is passed by address with no size information. (Key point: Extra arguments)

## Related in Computer Programming

- [Pass array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-largest-smallest) — Computer Programming, chapter 7
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Function returning largest of array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Alargest-from-array-function) — Computer Programming, chapter 7
- [Algorithm for largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-algorithm-largest-smallest) — Computer Programming, chapter 2
- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9
- [Pass array; transpose 3×3 matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-transpose-3x3) — Computer Programming, chapter 7

## Related in other subjects

- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
