write a program to insert an element into an array

Answer Posted / kartik choudhary

/* Program to insert element into array */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,n,pos,num;
clrscr();
printf("How Many Elements:-");
scanf("%d",&n);
printf("\n\tEnter Elements into Array");
for(i=0;i<n;i++)
{
scanf("\t%d",&arr[i]);
}
printf("\nArray Elements before inserting");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
printf("\nEnter the position");
scanf("%d",&pos);
if(pos==0 || pos>=n)
{
printf("Invalid position");
}
else
{
printf("\nEnter the number you want
to insert into an
array");
scanf("%d",&num);
/*shift the existing elements*/
for(i=n;i>=pos;i--)
arr[i]=arr[i-1];
arr[pos-1]=num;
n=n+1;/*this statement was missing*/
printf("\nArray elements after
insertion");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
}
getch();
}

Is This Answer Correct ?    0 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is token c++?

586


What are arithmetic operators?

544


Can non-public members of another instance of the class be retrieved by the method of the same class?

610


What is void pointer in c++ with example?

599


How do you master coding?

569






What is the difference between #import and #include in c++?

598


What is name hiding in c++?

617


What will happen if a pointer is deleted twice?

726


What is the maximum value of a unsigned char a) 255 b) 256 c) 128

636


Who was the creator of c++?

567


What are c++ tokens?

594


What is std :: flush?

578


What is setfill c++?

685


What is a storage class? Mention the storage classes in c++.

595


what Is DCS ? what i will get benefit when i did?

1838