Write a program to insert a number into an Array of 15 elements

/*
PROGRAM:
——–
Write a program to insert a number into an Array of 15 elements.
*/
#include<stdio.h>
#include<conio.h>

void main()
{
int a[10],pos,n,i;
int insert_element(int[],int);
clrscr();

for(i=0;i<7;i++)
{
printf(“Enter Number-%d: “,i+1);
scanf(“%d”,&a[i]);
printf(“\n”);
}

printf(“\n\nPlease enter the position at which you want to insert: “);
scanf(“%d”,&pos);

n=insert_element(a,pos-1);
if (n==-1)
printf(“\n\nInsertion Failure.”);
else
{
printf(“\n\nInsertion Successful.”);
}
getch();
}

int insert_element(int a[10],int pos)
{
int noe=7,lb=0,ub=9,k,key;
noe-=1;
printf(“\nPlease enter the number to insert: “);
scanf(“%d”,&key);

if(pos<lb || pos>noe || noe>=ub)
{
return(-1);
}
else
{
for(k=noe;k>=pos;k–)
{
a[k+1]=a[k];
}
a[pos]=key;
noe+=1;
printf(“\n\nThe Contents are: \n\n”);
for(k=0;k<=noe;k++)
{
printf(“%3d”,a[k]);
}
return(1);
}

}
/*
OUTPUT:
——-
Enter Number-1: 9

Enter Number-2: 8

Enter Number-3: 7

Enter Number-4: 6

Enter Number-5: 5

Enter Number-6: 4

Enter Number-7: 2

Please enter the position at which you want to insert: 4

Please enter the number to insert: 20
The Contents are:

9 8 7 20 6 5 4 2

Insertion Successful.

*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.