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
16 char chars[NUMBER_OF_RANDOM_LETTERS];
17
18
19 createArray(chars);
20
21
22 cout << "The lowercase letters are: " << endl;
23 displayArray(chars);
24
25
26 int counts[NUMBER_OF_LETTERS];
27
28
29 countLetters(chars, counts);
30
31
32 cout << "\nThe occurrences of each letter are: " << endl;
33 displayCounts(counts);
34
35 return 0;
36 }
37
38
39 void createArray(char chars[])
40 {
41
42
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
49 void displayArray(const char chars[])
50 {
51
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
62 void countLetters(const char chars[], int counts[])
63 {
64
65 for (int i = 0; i < NUMBER_OF_LETTERS; i++)
66 counts[i] = 0;
67
68
69 for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
70 counts[chars[i] - 'a'] ++;
71 }
72
73
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 }