write a program to insert an element into an array

Answer Posted / ranjit kumar

#include<stdio.h>
#include<conio.h>
void main()
{
int a[11],i,j,k,ch,sh,e;
clrscr();
printf("enter array elements:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
scanf("%d",&ch);
switch(ch)
{case1:
printf("enter the element:");
scanf("%d",&e);
printf("enter the element after which the number has to be inserted:");
scanf("%d",&sh);
for(i=0;i<10;i++)
{
if(a[i]==sh)//finding the element
break;
}
for(k=9;k>i;k--)
a[k+1]=a[k];//shifting the element
a[i+1]=e;
break;
case2:
printf("enter the element:");
scanf("%d",&e);
for(k=9;k>=0;k--)
a[k+1]=a[k];
a[0]=e;
break;
case3:
printf("enter the element:");
scanf("%d",&e);
a[10]=e;
}
printf("\n");
for(i=0;i<10;i++)//display the result
printf("%d",a[i]);
getch();
}

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you pass an array to a function in c++?

545


How should a contructor handle a failure?

690


What is the type of 'this' pointer?

605


What is the latest c++ standard?

688


Which bitwise operator is used to check whether a particular bit is on or off?

594






Should the this pointer can be used in the constructor?

558


What are the differences between new and malloc?

633


What are the comments in c++?

576


Are c and c++ similar?

592


How to declaring variables in c++?

663


Specify different types of decision control statements?

360


Write a program which employs Recursion

735


Can create new c++ operators?

575


Which software is used to run c++ program?

554


Write a short code using c++ to print out all odd number from 1 to 100 using a for loop

588