-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2nd.cpp
More file actions
155 lines (145 loc) · 3.73 KB
/
2nd.cpp
File metadata and controls
155 lines (145 loc) · 3.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//normal array operation like delete search,update
#include<bits/stdc++.h>
using namespace std;
typedef struct my_array
{
int *arr;//array in heap
int cap;//total capacity of array
int len;//current length of array
}my_array;
void bubble_sort(int arr[],int len)//sort array using bubble sort algorithm
{
for(int i=0;i<len-1;i++)
{
for(int j=0;j<len-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void display(my_array array)//displays the given array
{
for(int i=0;i<array.len;i++)
cout<<array.arr[i]<<" ";
cout<<"Capacity : "<<array.cap<<" Length : "<<array.len<<endl;
}
my_array del_array(my_array array,int ind)//deletes an element at a given index
{
if(ind==array.len-1)
{
array.len-=1;
}
else
{
for(int i=ind;i<array.len-1;i++)
array.arr[i]=array.arr[i+1];
array.len-=1;
}
return array;
}
my_array update(my_array array,int ind,int number)//updates the value at a given index
{
if(ind<0 || ind > array.len)
{
cout<<"Invalid Index"<<endl;
}
else
{
array.arr[ind]=number;
}
return array;
}
int search(int arr[],int n,int key)//returns 0 or 1 depending on key is present in array or not
{
for(int i=0;i<n;i++)
{
if(arr[i]==key)
return 1;
}
return 0;
}
int main()
{
my_array array;
cout<<"Enter the capacity of array :\n";
cin>>array.cap;
array.arr=new int[array.cap];
user_input://goto label
cout<<"Enter the number of elements you want to enter :\n";
cin>>array.len;
if(array.len<=array.cap)
{
for(int i=0;i<array.len;i++)
{
cout<<"Enter the elements at index "<<i<<endl;
cin>>*(array.arr+i);//using dereferencing pointer technique to input in array
}
}
else
{
cout<<"The number of elements in an array can't exceed its capacity.\n";
cout<<"Please enter again!!\n";
goto user_input;
}
while(true)
{
cout<<"Press 1 to delete an element at a given index\n";
cout<<"Press 2 to sort the array\n";
cout<<"press 3 to search if a key is present in array or not\n";
cout<<"Press 4 or any other key to exit\n";
cout<<"Enter choice : ";
int choice;
cin>>choice;
system("clear");
if(choice==1)
{
int ind;
cout<<"Enter index which you want to delete :\n";
cin>>ind;
if(ind<0)
{
cout<<"Invalid Index.\n";
}
else if(array.len==0)
{
cout<<"Array is already empty.\n";
}
else if(ind>array.len)
{
cout<<"Invalid Index.\n";
}
else
{
array=del_array(array,ind);
cout<<"Update array is : \n";
display(array);
}
}
else if(choice==2)
{
bubble_sort(array.arr,array.len);
cout<<"Update array is : \n";
display(array);
}
else if(choice == 3)
{
int key;
cout<<"Enter the key :\n";
cin>>key;
if(search(array.arr,array.len,key))
cout<<"The given key : "<<key<<" is present in array.\n";
else
cout<<"The given key : "<<key<<" is not present in array.\n";
}
else
{
cout<<"____________________________________BYE--------BYE_____________________________________"<<endl;
break;
}
}
}