C Programming

Inserting elements in an Array

An array is a systematic arrangement of objects, usually in rows and columns. ‘Inserting’ elements into an array might mean two things: inserting the value for a particular index (or indices) in the array (while still leaving the slot in the array open), or, actually inserting a slot (and its contents) into the array.

Language : C

[sourcecode language='c']
//TO INSERT AN ELEMENT OF THE ARRAY

#include
#include

void main()
{
int i,a[10],p,size,x;
char ch;
size=10;
clrscr();

do{
printf(“Enter the 10 elements of array”);
for(i=0;i<=10;i++)
{
scanf("%d",&a[i]);
}

printf("\nEnter the position to insert the elemaent of array");
scanf("%d",&p);
printf("\n Enter the element to insert");
scanf("%d",&x);

for(i=size;i>p;i–)
{
a[i]=a[i-1];
}
a[p]=x;

printf(“\n The new array is \n”);
for(i=0;i<=size;i++)
{
printf(“%d\t”,a[i]);
}

fflush(stdin);
printf(“\n\n Do you want to continue y/n”);
scanf(“%c”,&ch);
}while(ch==’y');

}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal