help in problem

--------------------------------
Last edited on
void main() BAD
int main(void) GOOD
also add return 0; after system("pause");

Does HHH count as 0 Heads pairs, 1 Heads pair, or 2 Heads pairs?
Have you learned about vectors?
Last edited on
HHH = 0 heads pairs
Last edited on
This should work for all cases. I added comments everywhere so hopefully it will be easy to understand.
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// Gets user input
void input(int &N, string &s){
    cout << "Welcome to Heads Or Tails program" << "\n";

    do {
        cout << "Please insert the number of tosses" << "\n";
        cin >> N;
    } while ((N < 0) || (N > 1000000));

    cout << "Now insert the tosses" << "\n";
    cin >> s;
    cout << endl;
}

// Calculates series information on the trials
void GetSeries(int &N, string &s, int &H, int &T, int &MaxH, int &MaxT, vector<int> &HSC, int &MHSCL, int &doubleH){
    // Cases where all trials are Heads
    if (s.find('T') == string::npos){
        doubleH = 0;
        MaxH = N;
        MaxT = 0;
        MHSCL = N;
    }
    //Cases where all trials are Tails
    else if (s.find('H') == string::npos) {
        doubleH = 0;
        MaxH = 0;
        MaxT = N;
        MHSCL = 0;
    }
    else {
        for (int i = 0; i < N;){
            H = 0; // Reset Heads series length
            while (s[i] == 'H'){ // Loops until the next character is not H
                H++;
                if ((H == 2) && (s[i + 1] != 'H')){ // Counts Heads pairs
                    doubleH++;
                }
                if (H > MaxH){ // Sets MaxHseriesLength
                    MaxH = H;
                }
                i++; // Go to next character
            }
            // Changes the vector to the size of the longest Heads series
            HSC.resize(MaxH, 0);
            // Takes the current Heads series length and adds +1 to that position in the vector
            HSC[H-1]++;

            // Resets Tails series length (unless the end of the trial has been reached)
            if (i != N) T = 0;
            while (s[i] == 'T'){ // Loops until the next character is not T
                T++;
                if (T > MaxT){ // Sets MaxTseriesLength
                    MaxT = T;
                }
                i++; // Go to next character
            }
        }
    }
}

// Calculates Mode
void GetMode(vector<int> &HSC, int &MaxHcount, int &MHSCL){
    for (int k = 0; k < HSC.size(); k++){
        // Finds highest number in HSC[]
        // An equal high number overwrites the previous
        if (HSC[k] >= MaxHcount){
            MHSCL = k+1;
            MaxHcount = HSC[k];
        }
    }
}

void output(int &doubleH, int &MaxH, int &MaxT, int &MHSCL){
    cout << "\nNumber of Heads pairs: ";
    cout << doubleH << endl;
    cout << "Length of longest Heads series: ";
    cout << MaxH << endl;
    cout << "Length of longest Tails series: ";
    cout << MaxT << endl;
    cout << "Most frequent Heads series length: ";
    cout << MHSCL << endl;
    cout << endl << endl;
}

int main(){
    int N;
    string s;
    int HseriesLength = 0; // Count of current Heads series
    int TseriesLength = 0; // Count of current Tails series
    int MaxHseriesLength = 0; // Longest Heads series
    int MaxTseriesLength = 0; // Longest Tails series
    vector<int> HseriesCount; // Position: series length, Value: frequeny
    int MaxHseriesCount = 0; // Frequency of the most frequent Heads series length
    int MaxHseriesCountLength = 0; // Length of the most frequent Heads series length
    int HpairsCount = 0; // Number of Heads pairs

    input(N, s);
    GetSeries(N, s, HseriesLength, TseriesLength, MaxHseriesLength, MaxTseriesLength, HseriesCount, MaxHseriesCountLength, HpairsCount);
    GetMode(HseriesCount, MaxHseriesCount, MaxHseriesCountLength);
    output(HpairsCount, MaxHseriesLength, MaxTseriesLength, MaxHseriesCountLength);

    return 0;
}
Thanks
It seems to work well
I will understand and test then go back
Thank you very much
Topic archived. No new replies allowed.