I have to display the most and least alphabetic character from a file. I decided to break the program into pieces so I could understand each process much better. This program however uses a simple character array that has characters a,b,c. I managed to compute the least and most common letter by using a lettercount array, but that array only holds the number of letter counts, how can I display the actual character?
using namespace std;
for (int i = 0; i < sizeof (letterArray); i++)
letter = letterArray[i];
lettercount[letter - 'a'] = lettercount[letter - 'a'] + 1; // this line was given by instructor
int smallest = lettercount;
for (int i = 1; i < sizeof(lettercount) / sizeof(lettercount); i++)
if (lettercount[i] < smallest)
smallest = lettercount[i];
cout << "The smallest number is: " << smallest << endl;
for (int i = 0; i < sizeof(lettercount)/ sizeof(lettercount); i++)
if (lettercount[i] > templargest)
templargest = lettercount[i];
cout << "the largest number is: " << templargest << endl;
To the computer, letters are just numbers. cout << (int) 'a' << " " << (int) 'A';
---> You can do arithmetic with numbers.
---> As Kemort says, your instructor's provided code shows how to bring ascii letters down to a zero-started index;
'a'-'a'=>0, 'b'-'a'=>1, 'c'-'a'=>2 etc., just reverse what he did to make;
0+'a'=>'a', 1+'a'=>'b', 2+'a'=>'c', etc.