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

# Pass array; largest and smallest

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2069 Asar, 2+6 marks
- Video: [C Program to find Largest and Smallest Element in an Array](https://www.youtube.com/watch?v=QHsSz-FEXto) — ScoreShala
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pass-array-largest-smallest
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Explain how array can be passed to functions. Write a program that passes an array to a function and print the largest and smallest element.

## Short answer

An array is passed by its name, which is the base address. The function initializes max and min to a[0], loops through all elements, updates both as needed, and prints them.

## Answer

#### Passing an array to a function
In C, an array is passed by giving only its name (without brackets) as the actual argument. The name stands for the address of the first element, so the function receives the address, not a copy of all elements (call by reference).

- Call: `findMinMax(a, n);`
- Definition: `void findMinMax(int a[], int n)` (or `int *a`). The size is passed separately because the function cannot know it.
- Any change made to `a[i]` inside the function changes the original array.
- For a 2D array, all dimensions except the first must be written in the parameter, for example `int m[][10]`.

#### Program

```c
#include <stdio.h>

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

int main(void)
{
    int a[100], n;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    printf("Enter the elements: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    findMinMax(a, n);          /* only the array name is passed */
    return 0;
}
```

**Sample run**

Input: `6 34 -5 78 12 0 56`

```text
Enter number of elements: Enter the elements: Largest = 78
Smallest = -5
```

## Check yourself

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

1. How is an array passed to a function in C?
   A. By copying every element into the parameter
   B. By writing only its name, which gives the address of the first element
   C. By writing the name with brackets in the call
   D. By passing the array's size only
   Answer: B. The name stands for the base address, so no copy is made. (Key point: Passing an array)

2. What happens if the function modifies a[i]?
   A. Only the local copy changes
   B. A compile error occurs
   C. The change is lost on return
   D. The original array in the caller changes
   Answer: D. The function works on the original memory. (Key point: Effect of changes)

3. Why is the size passed as a separate argument?
   A. Because arrays must be copied by size
   B. The function cannot know the array's size
   C. To allocate memory in the function
   D. To mark the end with '\0'
   Answer: B. Only an address arrives, so the length must be supplied. (Key point: Why pass n)

4. For a 2D array parameter, which size must be given?
   A. Only the first dimension
   B. None
   C. Only the total number of elements
   D. All dimensions except the first
   Answer: D. The compiler needs the column size to compute addresses. (Key point: 2D parameter)

## Related in Computer Programming

- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-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
- [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
- [Second largest element using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-second-largest) — Computer Programming, chapter 9

## 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
- [Two-element array and end fire pattern](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Atwo-isotropic-endfire-pattern) — Propagation and Antenna, chapter 2
