I have asked the same question a day ago, and I tried my best to fix the problem.
but even though I put a lot of time in it, haven't got the correct answer.
Please, help me. just giving hints and telling me to study that I haven't learned didn't really help me... I put at least 5 hours for this
9. duplicate for
all the element within the range low high,only print out one copy of the word when they are contiuous duplicates, along with the count of how many times this word appeared continuously.
void duplicate(const string source[], int low, int high);
For example, when the source array has the following words from index 0 to 9:
0: aa
1: aa
2: aa
3: bb
4: cc
5: cc
6: aa
7: aa
8: aa
9: aa
duplicate(words, 0, 9) would produce this output:
'aa' appeared 3 times.
'bb' appeared 1 times.
'cc' appeared 2 times.
'aa' appeared 4 times.
duplicate(words, 0, 2) would produce this output:
'aa' appeared 3 times.
duplicate(words, 2, 4) would produce this output:
'aa' appeared 1 times.
'bb' appeared 1 times.
'cc' appeared 1 times.
here is my code.
1 2 3 4 5 6 7 8 9
|
void duplicate(const string source[], int low, int high) {
int dupTimes = 1;
for (int i = low; i < high; i++ ) {
if (source[low] == source[low+1])
dupTimes++;
cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
dupTimes = 1;
}
}
|