0% found this document useful (0 votes)
3 views

array

The document provides C code snippets for inserting and deleting elements in an array. It explains how to insert a new element at a specified position and how to delete an element from a given index. The code includes user prompts for inputting the number of elements and the actual elements of the array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

array

The document provides C code snippets for inserting and deleting elements in an array. It explains how to insert a new element at a specified position and how to delete an element from a given index. The code includes user prompts for inputting the number of elements and the actual elements of the array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ques.

Array: Insertion and Deletion


1. Insertion
#include<stdio.h>
void main()
{
int a[20],m,i,j,k,new_ele;
printf("enter the number of elements:");
scanf("%d",&m);
printf("enter %d elements:",m);
for(k=1;k<=m;k++)
scanf("%d",&a[k]);

printf("enter the element to be inserted:");


scanf("%d",&new_ele);
printf("where %d is to be inserted:",new_ele);
scanf("%d",&j);

i=m;
while(i>=j)
{
a[i+1]=a[i];
i--;
}
a[j]=new_ele;
m=m+1;

printf("array after insertion:\n");


for(k=1;k<=m;k++)
printf("%d\n",a[k]);
}
a. First
b. Last

2. Deletion:
#include<stdio.h>
void main()
{
int a[20],m,i,j,k,del;
printf("enter the number of elements:");
scanf("%d",&m);
printf("enter %d elements:",m);
for(k=1;k<=m;k++)
scanf("%d",&a[k]);

printf("enter the element to be inserted:");


scanf("%d",&j);

i=j;
while(i<=m-1)
{
a[i]=a[i+1];
i++;
}
m=m-1;

printf("array after insertion:\n");


for(k=1;k<=m;k++)
printf("%d\n",a[k]);
}
a. First

b. Last

You might also like