-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdeleteRepeats.cpp
199 lines (141 loc) · 4.22 KB
/
deleteRepeats.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
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
197
198
199
//*****************************************************************************************************
// Delete Repeats in Array
//
// This program reads a list of characters from the user and then delete all the repeated
// characters in the list.
//
//*****************************************************************************************************
#include <iostream>
using namespace std;
//*****************************************************************************************************
const int SIZE_LIMIT = 1024;
void readArray(char[], int &);
void deleteRepeats(char[], int &);
void printArray(char[], int);
//*****************************************************************************************************
int main() {
char array[SIZE_LIMIT];
char answer;
int size;
do {
size = 0;
readArray(array, size);
deleteRepeats(array, size);
cout << "\n\nThe array after delete repeats";
printArray(array, size);
cout << "\n\nRepeat? (y/n): ";
cin >> answer;
} while (answer != 'n' && answer != 'N');
return 0;
}
//*****************************************************************************************************
void readArray(char array[], int &size) {
cout << "\nWhat is the size: ";
cin >> size;
while (size > SIZE_LIMIT || size < 0) {
cerr << "\nInvalid size. Please enter a size between 0 and " << SIZE_LIMIT;
cout << "\n\nWhat is the size: ";
cin >> size;
}
cout << "Enter the array (one character at a time): " << endl;
for (int i = 0; i < size; i++)
cin >> array[i];
}
//*****************************************************************************************************
void deleteRepeats(char array[], int &size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
while (array[i] == array[j]) {
cout << "\nFound duplicates at " << i + 1
<< " and " << j + 1 << " : " << array[i]
<< " and " << array[j] << endl;
for (int k = j + 1; k < size; k++)
array[k - 1] = array[k];
array[--size] = 0;
printArray(array, size);
}
}
}
}
//*****************************************************************************************************
void printArray(char array[], int size) {
cout << "\nUpdated array: ";
for (int i = 0; i < size; i++)
cout << array[i] << " ";
}
//*****************************************************************************************************
/*
What is the size: 10
Enter the array (one character at a time):
1
1
1
1
1
1
1
1
1
1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1 1 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1 1
Found duplicates at 1 and 2 : 1 and 1
Updated array: 1
The array after delete repeats
Updated array: 1
Repeat? (y/n): n
*****************************************************************************************************
What is the size: 5000
Invalid size. Please enter a size between 0 and 1024
What is the size: -20
Invalid size. Please enter a size between 0 and 1024
What is the size: 10
Enter the array (one character at a time):
a
a
a
t
5
0
3
j
n
a
Found duplicates at 1 and 2 : a and a
Updated array: a a t 5 0 3 j n a
Found duplicates at 1 and 2 : a and a
Updated array: a t 5 0 3 j n a
Found duplicates at 1 and 8 : a and a
Updated array: a t 5 0 3 j n
The array after delete repeats
Updated array: a t 5 0 3 j n
Repeat? (y/n): y
What is the size: 5
Enter the array (one character at a time):
1
2
3
1
2
Found duplicates at 1 and 4 : 1 and 1
Updated array: 1 2 3 2
Found duplicates at 2 and 4 : 2 and 2
Updated array: 1 2 3
The array after delete repeats
Updated array: 1 2 3
Repeat? (y/n): n
*/