re: count duplicate words (count array, void function)

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;
    }
}
Last edited on
line 3: it needs to be high-1 due to the +1 the next line
line 4: use i instead of low
line 6/7: This is the else case and after the loop
@coder777

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void duplicate(const string source[], int low, int high) {
    int dupTimes = 1;    
    for (int i = low; i < high-1; i++ ) {
        if (source[i] == source[i+1]) {
            dupTimes++;
        }
    }
    else {
    cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
    dupTimes = 1;
    }
    return;
}
}

error occur
main.cpp:8: error: expected `}' before 'else'
main.cpp: At global scope:
main.cpp:9: error: expected unqualified-id before 'else'
main.cpp:13: error: expected unqualified-id before 'return'
main.cpp:14: error: expected declaration before '}' token
put else after if inside the loop. After the loop there's no else and dupTimes = 1; is unnecessary as well.

you may need to put int i = low; before the loop
@coder777

1
2
3
4
5
6
7
8
9
10
11
12
void duplicate(const string source[], int low, int high) {
    int dupTimes = 1;
    int i = low;
    for (i = low; i < high-1; i++ ) {
        if (source[i] == source[i+1]) {
            dupTimes++;
        }
        else 
            cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;    
        }
    return;
}


Expected Output:

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
duplicate(words,·0,·2)↵
'a'·appeared·3·times.↵

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
[4]:·'b'↵
[5]:·'a'↵
[6]:·'a'↵
duplicate(words,·1,·5)↵
'a'·appeared·2·times.↵
'b'·appeared·2·times.↵
'a'·appeared·1·times.↵
------------------------------------------------------------------------------------------------
Actual Output:

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
duplicate(words,·0,·2)↵

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
[4]:·'b'↵
[5]:·'a'↵
[6]:·'a'↵
duplicate(words,·1,·5)↵
'a'·appeared·2·times.↵

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void duplicate(const string source[], int low, int high) {
    int dupTimes = 1;    
    i = low;
    for (; i < high-1; i++ ) {
        if (source[i] == source[i+1]) {
            dupTimes++;
        }
        else {
        cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
        dupTimes = 1;
        }
    }
    cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
}
@coder777
it seems like it's almost right.

Expected Output:

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
duplicate(words,·0,·2)↵
'a'·appeared·3·times.↵

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
[4]:·'b'↵
[5]:·'a'↵
[6]:·'a'↵
duplicate(words,·1,·5)↵
'a'·appeared·2·times.↵
'b'·appeared·2·times.↵
'a'·appeared·1·times.↵

Actual Output:

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
duplicate(words,·0,·2)↵
'a'·appeared·2·times.↵

array·is:↵
[0]:·'a'↵
[1]:·'a'↵
[2]:·'a'↵
[3]:·'b'↵
[4]:·'b'↵
[5]:·'a'↵
[6]:·'a'↵
duplicate(words,·1,·5)↵
'a'·appeared·2·times.↵
'b'·appeared·2·times.↵
@coder777
I changed high - 1 to high. and I got it correct
Thank you so much!
@coder777
here is the final code(correct)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void duplicate(const string source[], int low, int high) {
    int dupTimes = 1;    
    int i = low;
    for (; i < high; i++ ) {
        if (source[i] == source[i+1]) {
            dupTimes++;
        }
        else {
        cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
        dupTimes = 1;
        }
    }
    cout << "'" << source[i] << "'"<< " appeared " << dupTimes << " times." << endl;
}
Topic archived. No new replies allowed.