write a program to insert an element into an array
Answers were Sorted based on User's Feedback
Answer / vijesh kumar
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n,x,y,i,pos=0;
a[0]=1;
a[1]=3;
a[2]=4;
a[3]=5;
a[4]=6;
x=2;
y=1;
for(i=5+1;i>y;--i){
a[i]=a[i-1];
}
a[y]=x;
cout<<"\nAfter the inserting : ";
for(i=0;i<6;i++){
cout<<a[i]<<" ";
}
getch();
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / antony
/* Program to insert element into array */
#include<stdio.h>
#include<conio.h>
int n,pos;
void main()
{
int arr[50],i,ch;
void insert (int *);
void update (int *);
void delete (int *);
void search (int *);
void display (int *);
//clrscr();
printf("How Many Elements you will insert:-");
scanf("%d",&n);
printf("\n\tEnter Elements into Array");
for(i=0;i<n;i++)
scanf("\t%d",&arr[i]);
while(1)
{
printf("\n1.insert\n");
printf("2.Delete\n");
printf("3.Search\n");
printf("4.Display\n");
printf("5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(arr); display(arr); break;
case 2: delete(arr); display(arr); break;
case 3: search(arr); break;
case 4: display(arr); break;
case 5: exit(1);
}
}
getch();
}
void insert(int *arr)
{
int num,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;
}
}
void delete(int *arr)
{
int pos,i;
printf("\nEnter the position");
scanf("%d",&pos);
if(pos==0 || pos>=n)
{
printf("Invalid position");
}
else
{
for(i=pos-1;i<n;i++)
arr[i]=arr[i+1];
n--;
}
}
void display(int *arr)
{
int i;
printf("\nCurrent Array elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
}
void search(int *arr)
{
int i=0,tmp,tmp2=0;
printf("\nEnter the number u want to search:");
scanf("%d",&tmp);
for (i=0;i<=n;i++)
{
if(tmp==arr[i])
{
tmp2=i+1;
}
}
if (tmp2!=0)
{
printf("The element present in the position is: %d",tmp2);
}
else
{
printf("The element not present");
}
}
| Is This Answer Correct ? | 1 Yes | 2 No |
void insert(int*,int pos,int num);
void main()
{
int a[10],num,pos,i;
cout<<"Enter number which is to be inserted";
cin>>num;
cout<<"Enter position where no. is to be inserted";
cin>>pos;
insert(a,1,78);
insert(a,2,3);
getch();
}
void insert(int *a,int pos,int num)
{
int i;
for(i=9;i>=pos;i--)
{
a[i]=a[i-1];
}
a[i]=num;
}
| Is This Answer Correct ? | 2 Yes | 3 No |
Answer / 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 |
Answer / ajay
21.cout<<"Enter another element: ";
22.cin>>num;
23.cout<<"Enter position number: ";
24.cin>>pos;
25.cout<<endl;
26.--pos;
27.for(i=size;i>=pos;i--)
28.a[i+1]=a[i];
29.a[pos]=num;
30.size++;
31.cout<<"New Array: "<<endl;
32.for(i=0;i<size;i++)
33.cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / awais jamil
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int j,x[6]={1,2,3,4,5};
int i,pos;
cout<<"please enter for position"<<endl;
cin>>pos;
cout<<"enter for number"<<endl;
cin>>j;
for(i=4;i>=pos;i--)
{
x[i]=x[i-1];
}
x[pos]=j;
for(i=0;i<6;i++)
cout<<x[i]<<" ";
getch();
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / abhishek
This is just perfect, i compiled it right now!!
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,ent,ins;
clrscr();
printf(" enter how many nos u wanna putin");
scanf("%d",&n);
if(n<1||n>20)
{
printf(" invalid input");
return;
}
printf(" Enter the elements into the array one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the elements in the array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" Which address field will occupy the new element");
scanf("%d",&ins);--ins;
printf(" enter the elemnt u wanna put in");
scanf("%d",&ent);
for(i=n;i>=ins;i--)
{
a[i+1]=a[i];
}
a[ins]=ent; n++;
printf(" the new array after insertion of nu elemnt");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
| Is This Answer Correct ? | 4 Yes | 6 No |
Answer / kingkill
// The Process of adding new element into an array is known as insertion. Array can be inserted in the beginning or the end if the array is unsorted.
//Insertion can only be performed in an array if the memory space initially allocated is not full, i.e the number of elements in the array is less then the size of the array.
#include <iostream>
using namespace std;
#define MAX 20
int main ()
{
int A[MAX],i,pos,j,size,item;
cout<<" Enter the number of elements in the array : ";
cin>>size;
cout<<" Array size defined is "<<size<<"\n";
if(size>MAX) // Checks the array size ( defined )
{
cout<<" The Maximum Size is 20 \n";
}
cout<< " Enter the elements in sorted order: \n";
for(i=0;i<size;i++)// Elements inserted equal to size
{
cin>>A[i];
}
cout<<" Enter the element to be inserted : ";
cin>>item;
if(size==MAX) // Checks if free array space is free
{
cout<<"The Aray is Full \n";
}
for(i=0;i<size;i++)
{
if(item<A[i])
{
pos=i;
break;
}
}
if(i==size)
pos=size;
for(j=size;j>pos;j--)
A[j]=A[j-1];
A[j]=item;
size++;
cout<<" Array elements after insertion : ";
for(i=0;i<size;i++)
{
cout<<A[i];
}
cout<<"\n";
return 0;
| Is This Answer Correct ? | 1 Yes | 4 No |
Answer / 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 |
Answer / saranya
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
cout<<"enter the element to be inserted into an array";
for(i=0;i<5;i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
cout<<a[i];
}
getch();
}
| Is This Answer Correct ? | 21 Yes | 50 No |
What are the two types of comments?
What is called array?
List the types of polymorphism in c++?
What is #include iomanip?
How a new element can be added or pushed in a stack?
Explain the concept of inheritance in C++.
What does return 0 do in c++?
Is std :: string immutable?
What is the full name of logo?
If a function doesn’t return a value, how do you declare the function?
Are strings mutable in c++?
What is RTRT tool?can it be used for automation?can it work on packet PC?