-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (60 loc) · 1.97 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (60 loc) · 1.97 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
#include<string>
#include<iostream>
using namespace std;
string* addEntry(string *dynamicArray, size_t &size, string newEntry){
++size ;
string * NewArray = new string[size];
for(size_t j = 0; j <size-1 ; ++j) NewArray[j] = dynamicArray[j];
NewArray[size-1] = newEntry;
delete[] dynamicArray;
return NewArray;
};
string* deleteEntry(string *dynamicArray, size_t &size, string entryToDelete){
string * NewArray;
size_t i = 0;
while( i < size && dynamicArray[i] != entryToDelete)
++i ;
if(i == size){
NewArray = new string[size];
for(size_t j = 0; j <size; ++j)
NewArray[j] = dynamicArray[j];
}
else{
--size;
NewArray = new string[size];
for(size_t index = 0 ; index<i ;++index) NewArray[index]=dynamicArray[index];
for(size_t index = 0;index<size;++index)NewArray[index] = dynamicArray[index+1];
};
delete[] dynamicArray;
return NewArray;
};
void displaysarray(string* strings, size_t size){
cout<<"{ ";
for(size_t j =0; j < size - 1;++j) cout<<*(j+strings)<<", ";
cout<<*(strings+size-1)<<" }";
};
int main(){
size_t size = 5;
string* strings = new string[size]
{"ALaa", "Cole", "Lucii", "Lucca", "Lili"};
cout<<"the array before add or delete " << endl ;;
displaysarray(strings, size);
cout<<endl;;
strings = deleteEntry(strings, size, "ALaa");
cout << "the array after delete (alaa) " << endl;
displaysarray(strings, size);
cout<<endl ;
cout << "the array after delete (Cole) " << endl;
strings = deleteEntry(strings , size , "Cole") ;
displaysarray(strings , size ) ;
cout << endl ;
strings = addEntry(strings, size, "Alaa");
cout <<"the array after add (alaa) " << endl ;
displaysarray(strings, size);
cout<<endl ;
strings = addEntry(strings, size, "Cole");
cout <<"the array after add (Cole) " << endl ;
displaysarray(strings, size);
cout<< endl ;
return 0;
};