---
title: "Insert value in array at position"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:array-insert-algorithm"
source: https://www.bctengineer.com/q/semester_1/computer_programming/array-insert-algorithm
site: BCT Engineering
---

# Insert value in array at position

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2071 Chaitra, 4 marks
- Video: [Inserting an element in an array (C program)](https://www.youtube.com/watch?v=75ufMbIPcmg) — CSE GURUS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/array-insert-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write an algorithm to insert a value in an array at a position given by user.

## Short answer

If the array is not full and the position is valid, shift elements from the end down to that position one place right, insert the new value, and increment the element count.

## Answer

**Algorithm: insert a value at a given position**

Let A be the array with N elements (positions 1 to N), MAX the capacity, POS the position and VAL the new value.

1. Start.
2. Read N and the elements A[1] … A[N].
3. Read POS and VAL.
4. If N = MAX or POS < 1 or POS > N + 1, display "insertion not possible" and stop (overflow check).
5. Set I = N.
6. Repeat while I ≥ POS: set A[I + 1] = A[I]; set I = I − 1. (shift elements to the right)
7. Set A[POS] = VAL.
8. Set N = N + 1.
9. Display A[1] … A[N].
10. Stop.

**Example:** A = 10 20 30 40 50, POS = 3, VAL = 99. The elements 30, 40, 50 move one place right and 99 is stored at position 3, giving 10 20 99 30 40 50.

C implementation of the same algorithm:

```c
#include <stdio.h>

int main(void)
{
    int a[50], n, pos, val;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    printf("Enter elements: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    printf("Enter position (1 to %d) and value: ", n + 1);
    scanf("%d %d", &pos, &val);

    if (n >= 50 || pos < 1 || pos > n + 1) {
        printf("Insertion not possible\n");
        return 1;
    }
    for (int i = n - 1; i >= pos - 1; i--)   /* shift right */
        a[i + 1] = a[i];
    a[pos - 1] = val;
    n++;

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

Output:

Input: `5 10 20 30 40 50 3 99`

```text
Enter number of elements: Enter elements: Enter position (1 to 6) and value: New array: 10 20 99 30 40 50
```

## Check yourself

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

1. What check is made before inserting?
   A. N = MAX or POS out of range 1 to N+1
   B. Only POS > 0
   C. N = 0
   D. VAL > 0
   Answer: A. It stops if the array is full or position is invalid. (Key point: Overflow check)

2. How are elements moved to make room?
   A. Shifted left starting from the first
   B. Swapped in pairs
   C. Shifted one place right, starting from the last element
   D. Copied into a new array
   Answer: C. Starting from the end avoids overwriting. (Key point: Shifting)

3. After shifting, what is done?
   A. A[POS] = VAL and N = N + 1
   B. A[POS] = N
   C. N = N - 1
   D. A[1] = VAL
   Answer: A. Store the value and increase the count. (Key point: Final steps)

4. Insert 99 at position 3 in 10 20 30 40 50. Result?
   A. 10 99 20 30 40 50
   B. 10 20 30 99 40 50
   C. 10 20 99 30 40 50
   D. 99 10 20 30 40 50
   Answer: C. 30, 40, 50 move right and 99 takes position 3. (Key point: Example)

## Related in Computer Programming

- [Insert number at position in array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-insert-number-in-array) — Computer Programming, chapter 11
- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7
- [Standard deviation and mean of array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astandard-deviation-pseudocode) — Computer Programming, chapter 7

## Related in other subjects

- [Secant algorithm, versus false position](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asecant-pseudocode-vs-false-position) — Numerical Method, chapter 2
- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Insert before Kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-before-kth) — Data Structure and Algorithm, chapter 4
- [Insert and delete before a node, singly linked](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Asingly-insert-delete-before) — Data Structure and Algorithm, chapter 4
