Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Marilyn Sparks
- // This function
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- using namespace std;
- /* These are the function prototypes */
- void countLetters( const string& text, int letterCount[], char firstLetter, char lastLetter );
- int findMost ( int letter1array[], int size);
- int findNextMost (int letter2array[], int size, char MostIndex);
- int findLeast ( int letter3array[], int size);
- int findNextLeast ( int letter4array[], int size, char LeastIndex);
- int main()
- {
- /*Open file*/
- ifstream inFile ("INPUT.txt");
- if (!inFile) {
- cout << "Error opening file" << endl;
- exit(1);
- }
- /*Declare variables*/
- string theText;
- int MostIndex;
- int LeastIndex;
- int SecondMostIndex;
- int SecondLeastIndex;
- const char firstLetter = 'a';
- const char lastLetter = 'z';
- int size = lastLetter - firstLetter + 1;
- int letterCount[size];
- if( ! getline( inFile, theText, char(0) ) ) {
- exit(1);
- }
- countLetters (theText, letterCount, firstLetter, lastLetter);
- MostIndex = findMost(letterCount, size);
- SecondMostIndex = findNextMost(letterCount, size, MostIndex);
- LeastIndex = findLeast(letterCount, size);
- SecondLeastIndex = findNextLeast(letterCount, size, LeastIndex);
- /*Print out frequency table*/
- cout << "Most";
- cout.width(10);
- cout << "2nd Most";
- cout.width(11);
- cout << "2nd Least";
- cout.width(8);
- cout << "Least";
- cout << endl;
- cout.width(2);
- cout << MostIndex;
- cout.width(8);
- cout << SecondMostIndex;
- cout.width(11);
- cout << LeastIndex;
- cout.width(10);
- cout << SecondLeastIndex;
- cout << endl;
- cout << endl;
- /*Close file*/
- inFile.close();
- system("PAUSE");
- return 0;
- }
- void countLetters( const string& theText, int letterCount[], char firstLetter, char lastLetter ) {
- /*Read in data from file to variable*/
- for( int i(0); i<theText.length(); i++ ) {
- if( theText[i] >= firstLetter && theText[i] <= lastLetter ) {
- letterCount[theText[i]-firstLetter]++;
- }
- }
- }
- /*This function finds the most frequent letter*/
- int findMost ( int letterCount[], int size) {
- int count(0); /*First for loop counter*/
- int MostIndex;
- for (int i(0); i<size; i++) {
- if (count < letterCount[i]) {
- count = letterCount[i];
- MostIndex = i;
- }
- }
- return MostIndex;
- }
- /*This function finds the most frequent letter*/
- int findNextMost (int letterCount[], int size, int MostIndex) {
- int count2(0); /*Second for loop counter*/
- int SecondMostIndex;
- for (int i(0); i<size; i++) {
- if (count2 < letterCount[i] && i != MostIndex) {
- count2 = letterCount[i];
- SecondMostIndex = i;
- }
- }
- return SecondMostIndex;
- }
- /* This function finds the least frequent letter*/
- int findLeast (int letterCount[], int size) {
- int count3; /*Count for least frequent*/
- int LeastIndex;
- for (int i(0); i<size; i++) {
- if (count3 > letterCount[i]) {
- count3 = letterCount[i];
- LeastIndex = i;
- }
- }
- return LeastIndex;
- }
- /* This function finds the second least frequent letter*/
- int findNextLeast ( int letterCount[], int size, int LeastIndex) {
- int count4; /*Count for 2nd least frequent*/
- int SecondLeastIndex;
- for (int i(0); i<size; i++) {
- if (count4 > letterCount[i] && i != LeastIndex) {
- count4 = letterCount[i];
- SecondLeastIndex = i;
- }
- }
- return SecondLeastIndex;
- }
Add Comment
Please, Sign In to add comment