-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab.cpp
50 lines (48 loc) · 1.2 KB
/
lab.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
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
void insert(char a[],char m,int pos,int s)
{
if(pos<0 || pos>=s)
cout<<"Invalid Index."<<endl;
for(int i=0;i<s;++i)
{
if(i==pos)
a[i] = m;
else
a[i] = '0'; //initialized all other elements to 0 for clarity.
}
for(int i=0;i<s;++i)
cout<<a[i]<<" ";
cout<<endl;
}
void deletion(char a[],int pos,int s)
{
if(pos<0 || pos>=s)
cout<<"Invalid Index."<<endl;
for(int i=0;i<s;++i)
{
if(i==pos)
a[i] = '\0';
}
for(int i=0;i<s;++i)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int i,n,index,index2;
cout<<"Enter the total number of elements for the array : ";
cin>>n;
cout<<"\n";
char arr[n] = {0}; //created an empty list
char ch,ch2;
int size = sizeof(arr)/sizeof(arr[0]);
cout<<"Enter the element and its index to be inserted : ";
cin>>ch>>index;
insert(arr,ch,index,size); //performing insertion
cout<<"Enter the index of the element to be deleted : ";
cin>>index2;
deletion(arr,index2,size); //performing deletion
cout<<"This program is made by Jayant Singh,ECE Branch with Scholar No. 21U01041."<<endl;
return 0;
}