// Word count and frequency count // Written by Jim Wolfe #include #include #include #include using namespace std; typedef string wordbysize[500]; typedef int counters[500]; void SearchCount (string, wordbysize, counters, int &); int main () { wordbysize sizes[20]; // Assume no words with more than 20 letters counters counts[20]; int total[20] = {0}; ifstream infile; int len; int pick; string word; int most; int one; string mostWord; string filename; cout << "Enter the file name: "; cin >> filename; infile.open(filename.c_str()); infile >> word; while (!infile.eof()) // Read words, put in arrays and count { len = word.size(); SearchCount (word, sizes[len], counts[len], total[len]); infile >> word; } cout << "Length Distinct Frequency Most Frequent" << endl; for (pick = 1; pick < 20; pick++) // For each word length if (total[pick] > 0) { most = 0; for (one = 0; one < total[pick]; one++) // Find most frequent word and count if (counts[pick][one] > most) { most = counts[pick][one]; mostWord = sizes[pick][one]; } cout << setw(6) << pick << setw(10) << total[pick] << setw(11) << most << " " << mostWord << endl; } return 0; } // Search for word in appropriate array; if not there add it and set frequency to 1 void SearchCount (string word, wordbysize thisSize, counters howMany, int & count) { int pick = 0; while (pick < count && word != thisSize[pick]) pick++; if (pick == count) { thisSize[pick] = word; howMany[pick] = 1; count++; } else howMany[pick]++; // Count found ones }