---
title: "Array overflow/underflow; add two arrays"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:array-overflow-underflow-add"
source: https://www.bctengineer.com/q/semester_1/computer_programming/array-overflow-underflow-add
site: BCT Engineering
---

# Array overflow/underflow; add two arrays

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2073 Shrawan, 2+4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/array-overflow-underflow-add
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

What are overflow and underflow errors in context of array? Write a program to add corresponding elements of two arrays. The results should form a new array.

## Short answer

Array overflow: accessing beyond last index (size-1) or inserting into full array; underflow: accessing before first index or deleting from empty. C does not check bounds, causing undefined behavior. The program checks n is within bounds (≤ MAX) before reading, then computes c[i]=a[i]+b[i] for each element.

## Answer

#### Overflow and underflow in arrays
- **Array overflow**: trying to store or access an element beyond the last valid index (size − 1), for example writing `a[10]` in `int a[10]`, or inserting into an array that is already full.
- **Array underflow**: trying to access an element before the first one (index −1 or below), or deleting/removing an element from an empty array.

C does not check array bounds. Such access touches memory that belongs to other variables, which causes wrong results, corrupted data or a crash (undefined behaviour). The programmer must check the index or the element count before access (`if (n < MAX)` before insertion, `if (n > 0)` before deletion).

#### Program: add corresponding elements of two arrays
The size n is checked against the array limit (to avoid overflow) and `c[i] = a[i] + b[i]` forms the new array.

```c
#include <stdio.h>
#define MAX 50

int main(void)
{
    int a[MAX], b[MAX], c[MAX], n;
    printf("Enter number of elements (1-%d): ", MAX);
    scanf("%d", &n);
    if (n < 1 || n > MAX) {              /* guard against overflow */
        printf("Invalid size\n");
        return 1;
    }
    printf("Enter elements of first array: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    printf("Enter elements of second array: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &b[i]);

    for (int i = 0; i < n; i++)
        c[i] = a[i] + b[i];              /* corresponding elements */

    printf("Resultant array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", c[i]);
    printf("\n");
    return 0;
}
```

**Sample run**

Input: `4 1 2 3 4 10 20 30 40`

```text
Enter number of elements (1-50): Enter elements of first array: Enter elements of second array: Resultant array: 11 22 33 44
```

## Check yourself

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

1. What is array overflow?
   A. Accessing index 0
   B. Deleting from an empty array
   C. Accessing or inserting beyond the last valid index or into a full array
   D. Using a negative size
   Answer: C. For int a[10], a[10] is beyond the last index. (Key point: Overflow)

2. What is array underflow?
   A. Accessing before the first element or deleting from an empty array
   B. Writing beyond the last element
   C. Using an unsigned index
   D. Initialising with fewer values
   Answer: A. Index -1 or removing from empty storage is underflow. (Key point: Underflow)

3. Does C check array bounds?
   A. Yes, at run time with an error
   B. Yes, at compile time always
   C. No, so the programmer must check the index
   D. Only for 2D arrays
   Answer: C. Unchecked access gives undefined behaviour. (Key point: Bounds checks)

4. How is the new array formed?
   A. c[i] = a[i] + b[i]
   B. c[i] = a[i] * b[i]
   C. c[i+1] = a[i]
   D. c = a + b
   Answer: A. Corresponding elements are added. (Key point: Adding arrays)

## Related in Computer Programming

- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Add and subtract matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-add-subtract) — Computer Programming, chapter 11
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Two dimensional arrays in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-2d-array-syntax) — Computer Programming, chapter 11

## Related in other subjects

- [Queue overflow and underflow errors](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aqueue-overflow-underflow) — Data Structure and Algorithm, chapter 2
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
- [Antenna arrays and two-element expression](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Adefine-array-two-isotropic) — Propagation and Antenna, chapter 2
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
