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
What should main() return in c and c++?
What are exceptions c++?
What is the basic structure of c++ program?
What is a forward referencing and when should it be used?
What is using namespace std in cpp?
What is a c++ map?
Is c++ a high level language?
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?
What is abstraction with real time example?
List down the guideline that should be followed while using friend function.
What is math h in c++?
How can I disable the "echo" feature?
What are maps in c++?
What is meant by forward referencing and when should it be used?
What is the rule of three?