-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertinarray.cpp
More file actions
61 lines (60 loc) · 1.58 KB
/
Copy pathinsertinarray.cpp
File metadata and controls
61 lines (60 loc) · 1.58 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
void insert(int array[],int idx,int size,int num){
for(int i = size; i>=idx; i--){
array[i+1] = array[i];
}
array[idx] = num;
size++;
cout<<"Elements of array:";
for(int i = 0; i<size;i++){
cout<<array[i]<<" ";
}
cout<<endl;
cout<<"New size of array is:"<<size<<endl;
}
void deleteElement(int array[],int idx,int size){
for(int i = idx; i<size; i++){
array[i] = array[i+1];
}
size--;
cout<<"Elements of array:";
for(int i = 0; i<size;i++){
cout<<array[i]<<" ";
}
cout<<endl;
cout<<"New size of array is:"<<size<<endl;
}
int main(){
int arr[100],idx,size,num;
cout<<"Enter size of array:";
cin>>size;
if(size>100){
cout<<"Exceedslimit"<<endl;
}
else{
for(int i = 0; i<size;i++){
cout<<"Enter "<<i+1<<" th number of array:";
cin>>arr[i];
}
cout<<"Elements of array:";
for(int i = 0; i<size;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
int choice;
cout<<"If you want to add press 1 or else to delete press 0:";
cin>>choice;
if(choice==1){
cout<<"Enter the index where you want to enter the element:";
cin>>idx;
cout<<"Enter the element to enter:";
cin>>num;
insert(arr,idx,size,num);
}else{
cout<<"Enter the index where you want to delete the element:";
cin>>idx;
deleteElement(arr,idx,size);
}
}
}