This program is supposed to store random lowercase letters into an array and then count each letter occurence. My problem is that for some strange reason this program is only counting the first 15 letters of a 100 element array of chars.
#include<iostream>
using namespace std;
#include "time.h"
#include <vector>
#include <algorithm>
#include <iostream>
void countLetters(const std::vector<char>& letters, std::vector<int>& countsL);
void displayCounts(const std::vector<char>& letters, std::vector<int>& countsL);
constunsignedint NUMBER_OF_LETTERS(100);
constunsignedint LETTERS_IN_ALPHABET(26);
constunsignedint OFFSET_TO_a(97);
int main()
{
std::vector<char> letters(NUMBER_OF_LETTERS);
std::vector<int> countsL(LETTERS_IN_ALPHABET); // zero initialize vector with 26 elements
for(unsignedint i = 0; i < NUMBER_OF_LETTERS; ++i)
{
letters[i] = static_cast<char>(((rand() % LETTERS_IN_ALPHABET) + OFFSET_TO_a));
}
countLetters(letters, countsL);
displayCounts(letters, countsL);
return 0;
}
void countLetters(const std::vector<char>& letters, std::vector<int>& countsL)
{
char c('a');
unsignedint i(0);
do {
// count all occurrences of character c then increment c.
// increment i, and test i before next loop.
countsL[i++] = std::count(letters.begin(), letters.end(), c++);
}while(i < LETTERS_IN_ALPHABET);
}
void displayCounts(const std::vector<char>& letters, std::vector<int>& countsL)
{
unsignedint i(0);
// display the original, 20 per line.
constunsignedint NUM_PER_LINE(20);
do {
std::cout << letters[i++] << ' ';
if(0 == (i % NUM_PER_LINE))
{
std::cout << std::endl;
}
} while(i < NUMBER_OF_LETTERS);
char c('a');
i = 0;
do {
// display the character, then increment c to the next letter.
// display the count for that letter, increment i, and test
// i before next loop.
std::cout << c++ << "'s : " << countsL[i++] << std::endl;
}while(i < LETTERS_IN_ALPHABET);
}