C program to insert an element in an array

devquora
devquora

Posted On: Dec 23, 2020

C program to insert an element in an array

 

Overview:

Before starting this article, I will give you the general overview of this article. This Article included Array Definition and Why we use array? Moreover, I will show Demonstration for Insertion of element in a specific index. and write logic for How to insert an element at specific index? Furthermore, I will write code for C program to insert an element in an array. After all, I will show you the working of this program with output as conclusion.

Table of contents:

  1. What is an Array?
  2. What is a index of an Array?
  3. Why we use array in comparison to many variables?
  4. Demonstration for Insertion of element in a specific index
  5. How to insert an element at specific index?
  6. C program to insert an element in an array
  7. Conclusion

What is an Array?

An array is a container for the collection of items that stores these items at contiguous memory locations.

What is a index of an Array?

The index of an array is basically a pointer that is used to indicate which element in the array will be used.

Why we use array in comparison to many variables?

Array is a time saving container in programming that saves time and memory storage when we write a program. Suppose, You want to store 100 numbers in to the memory than you will declare 100 specific variables to store it but it is very time consuming and legthy process that increase the lines of code. Therefore, We will store these 100 numbers into a single variable known as array that continuously store it. Suppose, You want to save 1, 2, and 3 into memory.

The traditional method to store it is: 
int a=1; 
int b=2; 
int c=3; 
The alternative way to store it is as follows: int arr[3]= {1,2,3};

Demonstration for Insertion of element in a specific index.

Array Insertion demo

How to insert an element at specific index?

  • Step 1: First get the element to be inserted, say x
  • Step 2: Then get the position at which this element is to be inserted, say pos
  • Step 3: Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos.
  • Step 4: Insert the element x now at the position pos, as this is now empty.Take a variable and store a year into it as integer.

C program to check leap year

#include <stdio.h>
 
int main()
{
    int arr[100] = { 0 };
    int i, x, pos, n = 10;
 
    // initial array of size 10
    for (i = 0; i < 10; i++)
        arr[i] = i + 1;
    // print the original array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
 
    /*element to be inserted x = 50;
	position at which element is to be inserted pos = 5;
	increase the size by 1 n++; then shift elements forward*/
    for (i = n-1; i >= pos; i--)
        arr[i] = arr[i - 1];
    // insert x at pos
		arr[pos - 1] = x;
    // print the updated array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return 0;
}

program Output

Conclusion:

This Program insert 50 at 4th index to the given array.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..