Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
196 lines (163 loc) · 3.57 KB

File metadata and controls

196 lines (163 loc) · 3.57 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
using namespace std;
class Array {
private :
int size;
int length;
int *items;
public :
Array(int arrSize) {
size = arrSize;
length = 0;
items = new int[arrSize];
}
void Fill() {
int no_of_items;
cout << "How many itmes you want to fill: ";
cin >> no_of_items;
if (no_of_items > size) {
cout << "You cannot exceed the array size\n";
return;
}
for (int i=0; i < no_of_items; i++) {
cout << "Enter the item: ";
cin >> items[i];
length++;
}
}
void Display() {
cout << "The array content\n";
for (int i=0; i < length; i++) {
cout << items[i] << "\t";
}
cout << endl;
}
int getLength() {
return length;
}
int getSize() {
return size;
}
int Search(int key) {
int index = -1;
for (int i=0; i < length; i ++) {
if (key == items[i]){
index = i;
break;
}
}
return index;
}
void Append(int newItem) {
if (length < size) {
items[length] = newItem;
length++;
} else {
cout << "The array is full\n";
}
}
void Insert(int index, int newItem) {
if (!(index >= 0 && index < size)) {
cout << "Index out of array range\n";
return;
}
if (length < size) {
for (int i = length; i > index; i--) {
items[i] = items[i-1];
}
items[index] = newItem;
length++;
} else {
cout << "The array is full\n";
}
}
void Delete(int index) {
if (!(index >= 0 && index < size)) {
cout << "Index out of array range\n";
return;
}
for (int i = index; i < length-1; i++)
items[i] = items[i+1];
items[length-1] = 0;
length--;
}
void Enlarge(int newSize) {
if (newSize <= size) {
cout << "Error - the new size must be larger than the old size\n";
return;
}
size = newSize;
int *temp = items;
items = new int[newSize];
for (int i=0; i <length; i++){
items[i] = temp[i];
}
delete[]temp;
}
void Merge(Array other) {
int newSize = other.getSize() + size;
size = newSize;
int *temp = items;
items = new int[newSize];
int i;
for (i=0;i<length; i++)
items[i] = temp[i];
delete[]temp;
int j=i;
for (int i=0; i<other.getLength(); i++){
items[j++] = other.items[i];
length++;
}
delete[]other.items;
}
};
int main() {
cout << "ADT for array\n";
int arrSize;
cout << "Enter the array size: ";
cin >> arrSize;
Array myArray(arrSize);
myArray.Fill();
cout << "Array size = " << myArray.getSize() << " while the length = " << myArray.getLength() << endl;
myArray.Display();
// Search
int key;
cout << "Enter the item youre loooking for: ";
cin >> key;
int index = myArray.Search(key);
if (index == -1){
cout << "Item not found!\n";
} else {
cout << "The item index is: " << index<< endl;
}
// Append
int newItem;
cout << "Enter item to append to the array: ";
cin >> newItem;
myArray.Append(newItem);
myArray.Display();
// Insert
int insItem, position;
cout << "Insert item: ";
cin >> insItem;
cout << "Position of item: ";
cin >> position;
myArray.Insert(position, insItem);
myArray.Display();
int del;
cout << "Delete item at index: ";
cin >> del;
myArray.Delete(del);
myArray.Display();
int newSize;
cout << "Enter the new size: ";
cin >> newSize;
myArray.Enlarge(newSize);
cout << "Array size = " << myArray.getSize() << " while the length = " << myArray.getLength() << endl;
myArray.Display();
Array other(3);
other.Fill();
myArray.Merge(other);
cout << "Array size = " << myArray.getSize() << " while the length = " << myArray.getLength() << endl;
myArray.Display();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.