write a program to insert an element into an array

Answer Posted / jyoti sur

#include<stdio.h>
#include<conio.h>
void main()
{
int pos,k,n,a[30];
int i;
clrscr();
printf("enter no.of element in array\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("your array is:\n");
for(i=0;i<n;i++)
{
printf("%6d",a[i]);
}
printf("\nenter the no.for insertion\n");
scanf("%d",&k);
printf("\n\nenter the position");
scanf("%d",&pos);
--pos;

if(pos<n)
{
for(i=n;i>=pos;i--)

a[i+1]=a[i];
a[pos]=k;
n++;

}
else
printf("\n outside the array");
printf("the new array is:");
for(i=0;i<n;i++)
{
printf("%6d",a[i]);
}
}

Is This Answer Correct ?    91 Yes 20 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the best way to take screenshots of a window with c++ in windows?

665


Is it possible to write a c++ template to check for a function's existence?

660


What is c++ runtime?

735


What is #include cstdlib in c++?

769


Explain static and dynamic memory allocation with an example each.

726






What is difference between malloc()/free() and new/delete?

733


Of the numbers 12 23 9 28 which would be at the top of a properly implemented maxheap a) 28 b) 9 c) Any of them could be

1048


How do you compile the source code with your compiler?

706


Why is that unsafe to deal locate the memory using free( ) if it has been allocated using new?

722


Should I learn c++ c?

705


What is flush programming?

661


What is a pointer how and when is it used?

676


Explain the differences between list x; & list x();.

682


What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero

754


What is abstraction c++?

676