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
If a base class is an adt, and it has three pure virtual functions, how many of these functions must be overridden in its derived classes?
What is the use of structure in c++?
What is the latest c++ version?
Comment on c++ standard exceptions?
How many types of modularization are there in c++?
If dog is a friend of boy, and terrier derives from dog, is terrier a friend of boy?
What is auto used for in c++?
Explain the extern storage classes in c++.
What is the use of endl?
Define Virtual function in C++.
Difference between pass by value and pass by reference?
Explain the problem with overriding functions
What is the use of lambda in c++?
What are the 2 main types of data structures?
What is the disadvantage of using a macro?