Hello,

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?

#include <iostream>

#include <string>

using namespace std;

int main()

{

int lettercount[3] = { 0 };

char letterArray[] = { 'a', 'b', 'b', 'b', 'a', 'c', 'c', 'c', 'c', 'c', }; //2as, 3bs, 5cs

char letter;

int templargest = 0;

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[0];

for (int i = 1; i < sizeof(lettercount) / sizeof(lettercount[0]); i++)

{

if (lettercount[i] < smallest)

{

smallest = lettercount[i];

}

}

cout << "The smallest number is: " << smallest << endl;

for (int i = 0; i < sizeof(lettercount)/ sizeof(lettercount[0]); i++)

{

if (lettercount[i] > templargest)

{

templargest = lettercount[i];

}

}

cout << "the largest number is: " << templargest << endl;

return 0;

}

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?

#include <iostream>

#include <string>

using namespace std;

int main()

{

int lettercount[3] = { 0 };

char letterArray[] = { 'a', 'b', 'b', 'b', 'a', 'c', 'c', 'c', 'c', 'c', }; //2as, 3bs, 5cs

char letter;

int templargest = 0;

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[0];

for (int i = 1; i < sizeof(lettercount) / sizeof(lettercount[0]); i++)

{

if (lettercount[i] < smallest)

{

smallest = lettercount[i];

}

}

cout << "The smallest number is: " << smallest << endl;

for (int i = 0; i < sizeof(lettercount)/ sizeof(lettercount[0]); i++)

{

if (lettercount[i] > templargest)

{

templargest = lettercount[i];

}

}

cout << "the largest number is: " << templargest << endl;

return 0;

}

closed account (*48T7M4Gy*)

The answer is all in the index value. You know the index of the largest value and you know there is a direct relationship, given by your instructor, between the index value and a letter.

To the computer, letters are just numbers.

---> 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.

`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.

`cout << 2 + ``'a'`;

Would output 'c'.
Last edited on

Topic archived. No new replies allowed.