SlideShare a Scribd company logo
3
Most read
6
Most read
Write a program to insert an element
into an array at a given position.
Agenda
1. Program in c++
2. Array declaration and definition
3. Insert new element at the specified position
4. Print the array
1. Before Insertion
2. After Insertion
Logic
• Given array for example:
• arr[]={1,2,3,4,5}
• Specify position at which position the num. to be
present ex:2
• Enter what element to be inserted:6
• Loop:
• for(i=n; i>pos-1;i--)
• { arr[i]=arr[i-1]}
• arr[pos-1]=ele
• Print Array before insertion and after insertion
Let’s See Practically by writing the
program
• #include<iostream.h>
• #include<conio.h>
• void main()
• {
• int a[100],ele,n,i,pos;
• clrscr();
• cout<<"Enter the size of array"<<endl;
• cin>>n;
• cout<<"Enter the elements of array"<<endl;
• for(i=0;i<n;i++)
• {
• cin>>a[i];
• }
• cout<<"Enter the position to insert element"<<endl;
• cin>>pos;
• if(pos<n)
• {
• cout<<"Enter the element to be inserted"<<endl;
• cin>>ele;
• cout<<"Array before insertion:"<<endl;
• for(i=0;i<n;i++)
• {
• cout<<a[i]<<" ";
• }
• cout<<endl;
• for(i=n;i>pos-1;i--)
• {
• a[i]=a[i-1];
• }
• a[pos-1]=ele;
• cout<<"Array after insertion:"<<endl;
• for(i=0;i<=n;i++)
• {
• cout<<a[i]<<" ";
• }
• }
• else
• cout<<"Invalid position"<<endl;
• getch();
• }
Thank You

More Related Content

What's hot (20)

PDF
PyLecture2 -NetworkX-
Yoshiki Satotani
 
PPTX
Sorting Technique
Salman Vadsarya
 
PPT
Grand Central Dispatch
Joachim Bengtsson
 
PPTX
Array
Iama Marsian
 
PDF
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
Andrea Lazzarotto
 
PPTX
Programming in c
CS_GDRCST
 
PDF
Insertion Sort Code
Nanthachai Poolketgij
 
PPTX
Call by value
Dharani G
 
DOCX
Bubble in link list
university of Gujrat, pakistan
 
DOCX
Java
AdamZidan3
 
DOCX
Assignment no 5
nancydrews
 
PDF
Queue
A. S. M. Shafi
 
PPT
Coherence SIG: Advanced usage of indexes in coherence
aragozin
 
PDF
Extending built in objects
Muhammad Ahmed
 
PPT
Bitwise Operators in C
yndaravind
 
PPTX
Designing a Program
Cornerstone University
 
DOCX
Programming-I assignment for BSCS students , NCBA&e
hamna ali
 
PDF
Check whether an Array is sorted or not
Ravi Ranjan
 
PDF
4 heapsort pq
hasan Mohammad
 
PyLecture2 -NetworkX-
Yoshiki Satotani
 
Sorting Technique
Salman Vadsarya
 
Grand Central Dispatch
Joachim Bengtsson
 
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
Andrea Lazzarotto
 
Programming in c
CS_GDRCST
 
Insertion Sort Code
Nanthachai Poolketgij
 
Call by value
Dharani G
 
Bubble in link list
university of Gujrat, pakistan
 
Assignment no 5
nancydrews
 
Coherence SIG: Advanced usage of indexes in coherence
aragozin
 
Extending built in objects
Muhammad Ahmed
 
Bitwise Operators in C
yndaravind
 
Designing a Program
Cornerstone University
 
Programming-I assignment for BSCS students , NCBA&e
hamna ali
 
Check whether an Array is sorted or not
Ravi Ranjan
 
4 heapsort pq
hasan Mohammad
 

Similar to Insert element position (20)

PPTX
Sorting elements increasing order
Kavya Shree
 
PPT
Arrays in C++
Janpreet Singh
 
DOCX
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
PPTX
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
DOC
A sorted linear array
Suneel Dogra
 
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
PPTX
Arrays
shillpi29
 
PDF
Array (data structure using c++).PPT presentation
minedrugs777
 
PPTX
Arrays_in_c++.pptx
MrMaster11
 
DOC
Main ds manual
Vivek Kumar Sinha
 
PDF
1D Array
Swarup Boro
 
DOCX
Updated Lab3.docx
AleezaAnjum
 
PPT
Lecture#8 introduction to array with examples c++
NUST Stuff
 
PDF
Chapter12 array-single-dimension
Deepak Singh
 
PPTX
Arrays in C.pptx
HarsimranKaur362773
 
PPTX
Array programs
ALI RAZA
 
PDF
C++ Arrays different operations .pdf
manahilzulfiqar6
 
PDF
Data structures arrays
maamir farooq
 
Sorting elements increasing order
Kavya Shree
 
Arrays in C++
Janpreet Singh
 
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
A sorted linear array
Suneel Dogra
 
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
Arrays
shillpi29
 
Array (data structure using c++).PPT presentation
minedrugs777
 
Arrays_in_c++.pptx
MrMaster11
 
Main ds manual
Vivek Kumar Sinha
 
1D Array
Swarup Boro
 
Updated Lab3.docx
AleezaAnjum
 
Lecture#8 introduction to array with examples c++
NUST Stuff
 
Chapter12 array-single-dimension
Deepak Singh
 
Arrays in C.pptx
HarsimranKaur362773
 
Array programs
ALI RAZA
 
C++ Arrays different operations .pdf
manahilzulfiqar6
 
Data structures arrays
maamir farooq
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
Dimensions of Societal Planning in Commonism
StefanMz
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Ad

Insert element position

  • 1. Write a program to insert an element into an array at a given position.
  • 2. Agenda 1. Program in c++ 2. Array declaration and definition 3. Insert new element at the specified position 4. Print the array 1. Before Insertion 2. After Insertion
  • 3. Logic • Given array for example: • arr[]={1,2,3,4,5} • Specify position at which position the num. to be present ex:2 • Enter what element to be inserted:6 • Loop: • for(i=n; i>pos-1;i--) • { arr[i]=arr[i-1]} • arr[pos-1]=ele • Print Array before insertion and after insertion
  • 4. Let’s See Practically by writing the program
  • 5. • #include<iostream.h> • #include<conio.h> • void main() • { • int a[100],ele,n,i,pos; • clrscr(); • cout<<"Enter the size of array"<<endl; • cin>>n; • cout<<"Enter the elements of array"<<endl; • for(i=0;i<n;i++) • { • cin>>a[i]; • } • cout<<"Enter the position to insert element"<<endl; • cin>>pos; • if(pos<n) • { • cout<<"Enter the element to be inserted"<<endl; • cin>>ele; • cout<<"Array before insertion:"<<endl; • for(i=0;i<n;i++) • { • cout<<a[i]<<" "; • } • cout<<endl; • for(i=n;i>pos-1;i--) • { • a[i]=a[i-1]; • } • a[pos-1]=ele; • cout<<"Array after insertion:"<<endl; • for(i=0;i<=n;i++) • { • cout<<a[i]<<" "; • } • } • else • cout<<"Invalid position"<<endl; • getch(); • }