Hey, guys. I'm working on a program that's supposed to take a phrase that the user inputs, then display the frequency of all letters appearing within that phrase, from most frequent to least frequent. So far, I've managed to get it to display the frequency of the letters in alphabetical order, but I cannot get it to display the results from most to least. Here's what I have so far.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int alphabet = 26;
void sort(int a[], int number);
void sort2(char a[], int number);
int main(){
char disp, limit = '.';
char sentence[255];
int letters = 0, counters[alphabet] = {0};
cout << "This program will take an input phrase of lowercase letters \n";
cout << "and count the frequency of every letter that it is composed of.\n";
cout << "Please type in a sentence composed of all lowercase letters, \n";
cout << "and include a period at the end: ";
char next;
for (int i = 0; next != limit; i++) {
cin.get(next);
letters++;
sentence[i] = next; }
sort(counters, letters);
for(int i = 0; sentence[i] > 0; i++) {
counters[sentence[i]-'a']++; }
cout << endl;
cout << "Letter Frequency\n" << endl;
for (int i = 0; i < alphabet; i++) {
if (counters[i] > 0) {
disp = i + 'a';
cout << disp << setw(13) << counters[i] << endl; }}
cout << endl;
system("pause");
return 0;
}
void sort(int a[], int number){
int temp, i, j;
for ( i = 0; i < number; ++i )
{
for ( j = i; j < number; ++j )
{
if ( a[i] > a[j] )
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
Anyone have some ideas? Thanks in advance.