top of page
Click here to go to the home page of AskTheCode.

Insert an element at a Specified Position in a given Array

Team ATC

Random Post

 

#include<stdio.h>

int insert(int [], int, int, int);

int main()

{

int i,n,num,pos;

int arr[30];

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the values below: \n");

for(i=0;i<n;i++)

{

printf("\nElement [%d] : ",i+1);

scanf("%d",&arr[i]);

}

printf("\nEnter the element you want to insert: ");

scanf("%d",&num);

printf("\nEnter the position where you want the insertion: \n");

scanf("%d",&pos);

insert(arr, n, num, pos);

}

int insert(int a[], int n, int num, int pos)

{

int i;

for(i=n;i>=pos;i--)

a[i]=a[i-1];

a[pos-1]=num;

printf("\nThe array after insertion is: ");

for(i=0;i<=n;i++)

printf("%d, ",a[i]);

return 0;

}

Recent Posts

See All

Kommentare


bottom of page