write a program to insert an element into an array

Answer Posted / meena

/* 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;
printf("\nArray elements after insertion");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
}
getch();
}

Is This Answer Correct ?    57 Yes 17 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What should main() return in c and c++?

614


What are exceptions c++?

681


What is the basic structure of c++ program?

660


What is a forward referencing and when should it be used?

671


What is using namespace std in cpp?

639






What is a c++ map?

768


Is c++ a high level language?

642


How can you quickly find the number of elements stored in a static array? Why is it difficult to store linked list in an array?

664


What is abstraction with real time example?

729


List down the guideline that should be followed while using friend function.

729


What is math h in c++?

689


How can I disable the "echo" feature?

696


What are maps in c++?

591


What is meant by forward referencing and when should it be used?

653


What is the rule of three?

658