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

Explain the difference between overloading and overriding?

607


What are member functions used in c++?

550


Difference between pointer to constant and constant pointer to a constant. Give example.

637


Do you know about C++ 11 standard?

631


Is eclipse good for c++?

536






How a new element can be added or pushed in a stack?

579


Do you know the problem with overriding functions?

570


Differences between private, protected and public and give examples.

577


What are 2 ways of exporting a function from a dll?

609


What is the extraction operator and what does it do?

608


what is COPY CONSTRUCTOR and what is it used for?

617


What is array in c++ pdf?

551


A mXn matrix is given and rows and column are sorted as shown below.Write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

3237


What is a Default constructor?

913


Explain about templates of C++.

674