1  #include <iostream>
  2  #include <ctime>
  3  #include <cstdlib>
  4  using namespace std;
  5  
  6  const int NUMBER_OF_LETTERS = 26;
  7  const int NUMBER_OF_RANDOM_LETTERS = 100;
  8  void createArray(char []);
  9  void displayArray(const char []);
 10  void countLetters(const char [], int []);
 11  void displayCounts(const int []);
 12  
 13  int main()
 14  {
 15    // Declare and create an array
 16    char chars[NUMBER_OF_RANDOM_LETTERS];
 17  
 18    // Initialize the array with random lowercase letters
 19    createArray(chars);
 20  
 21    // Display the array
 22    cout << "The lowercase letters are: " << endl;
 23    displayArray(chars);
 24  
 25    // Count the occurrences of each letter
 26    int counts[NUMBER_OF_LETTERS];
 27  
 28    // Count the occurrences of each letter
 29    countLetters(chars, counts);
 30  
 31    // Display counts
 32    cout << "\nThe occurrences of each letter are: " << endl;
 33    displayCounts(counts);
 34  
 35    return 0;
 36  }
 37  
 38  // Create an array of characters
 39  void createArray(char chars[])
 40  {
 41    // Create lowercase letters randomly and assign
 42    // them to the array
 43    srand(time(0));
 44    for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
 45      chars[i] = static_cast<char>('a' + rand() % ('z' - 'a' + 1));
 46  }
 47  
 48  // Display the array of characters
 49  void displayArray(const char chars[])
 50  {
 51    // Display the characters in the array 20 on each line
 52    for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
 53    {
 54      if ((i + 1) % 20 == 0)
 55        cout << chars[i] << " " << endl;
 56      else
 57        cout << chars[i] << " ";
 58    }
 59  }
 60  
 61  // Count the occurrences of each letter
 62  void countLetters(const char chars[], int counts[])
 63  {
 64    // Initialize the array
 65    for (int i = 0; i < NUMBER_OF_LETTERS; i++)
 66      counts[i] = 0;
 67  
 68    // For each lowercase letter in the array, count it
 69    for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
 70      counts[chars[i] - 'a'] ++;
 71  }
 72  
 73  // Display counts
 74  void displayCounts(const int counts[])
 75  {
 76    for (int i = 0; i < NUMBER_OF_LETTERS; i++)
 77    {
 78      if ((i + 1) % 10 == 0)
 79        cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
 80      else
 81        cout << counts[i] << " " << static_cast<char>(i + 'a') << " ";
 82    }
 83  }