-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_insertion.cpp
38 lines (31 loc) · 892 Bytes
/
10_insertion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Coding insertion operation in array in data structures in C++ language.
#include<iostream>
using namespace std;
void display(int arr[] , int used_size){
// Code for Traversal
for(int i = 0 ; i<used_size ; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int IndexInsertion(int arr[] , int size , int element , int capacity ,int index){
// Code for insertion
if(size>=capacity){
return -1;
}
for(int i = size-1 ; i>=index ;i--){
arr[i+1] = arr[i];
}
arr[index] = element;
return 1;
}
int main(){
int arr[100] = {7,8,12,27,88};
int size = 5 , element = 45 , index = 3;
display(arr , size);
IndexInsertion(arr , size , element , 100 ,index);
size += 1; // size = size + 1;
display(arr , size);
return 0;
}
// Quiz : If insertion is successful then print array otherwise print insertion is failed.