This example will show you how to insert an element into an Array using C program.
The below insert function takes four arguments
void insert(int *a, int n, int i, int k)
*a – pointer to an array
n – size of the array
i – at what position of the array the element will be inserted
k – the element to be searched in array
Example
#include<stdio.h> void insert(int *a, int n, int i, int k){ int j,l; if(n<=i){ printf("Insertion index must be less than size of the array"); return; } for(j=n-2;j>=i;j--){ a[j+1]=a[j]; } a[j+1]=k; for(l=0;l<n;l++){ printf("a[%d]=%d ",l,a[l]); } } main(){ int i; int size=6; int a[6]; for(i=0;i<size-1;i++){ a[i]=i+100; } insert(a,size,5,135); return; }
Output
a[0]=100 a[1]=101 a[2]=102 a[3]=103 a[4]=104 a[5]=135
No comments:
Post a Comment