Array help

I am generating random characters (A-Z) and I am trying to figure out how to count the amount of times each character appears. I believe what I need to do is put the generated letters into an array but I am not sure how to do that and then I am not sure where to go after that. Any help would be appreciated!!
1
2
3
4
5
6
7
8
9
10
11
const int NbLetters = 'Z' - 'A' + 1; // It'll be 26

int CharCount[NbLetters] = {0};

while (true)
{
    // Generate random letter between 'A' and 'Z'
    char somechar = char(rand() % NbLetters) + 'A';

    ++CharCount[someChar - 'A'];
}


Or a little simpler:
1
2
3
4
5
int CharCount[26] = {0};
while (true)
{
    ++CharCount[rand()%26];
}


Then to interpret the results:
1
2
for (char letter = 'A'; letter <= 'Z'; ++letter)
    cout << "The letter " << letter << " appeared " << CharCount[letter - 'A'] << " times.\n";
Last edited on
Topic archived. No new replies allowed.