Guest User

Untitled

a guest
Apr 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. // Marilyn Sparks
  2. // This function
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. /* These are the function prototypes */
  11. void countLetters( const string& text, int letterCount[], char firstLetter, char lastLetter );
  12. int findMost ( int letter1array[], int size);
  13. int findNextMost (int letter2array[], int size, char MostIndex);
  14. int findLeast ( int letter3array[], int size);
  15. int findNextLeast ( int letter4array[], int size, char LeastIndex);
  16.  
  17. int main()
  18. {
  19. /*Open file*/
  20. ifstream inFile ("INPUT.txt");
  21. if (!inFile) {
  22. cout << "Error opening file" << endl;
  23. exit(1);
  24. }
  25.  
  26. /*Declare variables*/
  27. string theText;
  28. int MostIndex;
  29. int LeastIndex;
  30. int SecondMostIndex;
  31. int SecondLeastIndex;
  32. const char firstLetter = 'a';
  33. const char lastLetter = 'z';
  34. int size = lastLetter - firstLetter + 1;
  35. int letterCount[size];
  36.  
  37. if( ! getline( inFile, theText, char(0) ) ) {
  38. exit(1);
  39. }
  40.  
  41. countLetters (theText, letterCount, firstLetter, lastLetter);
  42.  
  43. MostIndex = findMost(letterCount, size);
  44.  
  45. SecondMostIndex = findNextMost(letterCount, size, MostIndex);
  46.  
  47. LeastIndex = findLeast(letterCount, size);
  48.  
  49. SecondLeastIndex = findNextLeast(letterCount, size, LeastIndex);
  50.  
  51. /*Print out frequency table*/
  52. cout << "Most";
  53. cout.width(10);
  54. cout << "2nd Most";
  55. cout.width(11);
  56. cout << "2nd Least";
  57. cout.width(8);
  58. cout << "Least";
  59. cout << endl;
  60. cout.width(2);
  61. cout << MostIndex;
  62. cout.width(8);
  63. cout << SecondMostIndex;
  64. cout.width(11);
  65. cout << LeastIndex;
  66. cout.width(10);
  67. cout << SecondLeastIndex;
  68. cout << endl;
  69. cout << endl;
  70.  
  71. /*Close file*/
  72. inFile.close();
  73. system("PAUSE");
  74. return 0;
  75. }
  76.  
  77. void countLetters( const string& theText, int letterCount[], char firstLetter, char lastLetter ) {
  78. /*Read in data from file to variable*/
  79. for( int i(0); i<theText.length(); i++ ) {
  80. if( theText[i] >= firstLetter && theText[i] <= lastLetter ) {
  81. letterCount[theText[i]-firstLetter]++;
  82. }
  83. }
  84. }
  85.  
  86. /*This function finds the most frequent letter*/
  87. int findMost ( int letterCount[], int size) {
  88. int count(0); /*First for loop counter*/
  89. int MostIndex;
  90. for (int i(0); i<size; i++) {
  91. if (count < letterCount[i]) {
  92. count = letterCount[i];
  93. MostIndex = i;
  94. }
  95. }
  96. return MostIndex;
  97. }
  98.  
  99. /*This function finds the most frequent letter*/
  100. int findNextMost (int letterCount[], int size, int MostIndex) {
  101. int count2(0); /*Second for loop counter*/
  102. int SecondMostIndex;
  103. for (int i(0); i<size; i++) {
  104. if (count2 < letterCount[i] && i != MostIndex) {
  105. count2 = letterCount[i];
  106. SecondMostIndex = i;
  107. }
  108. }
  109. return SecondMostIndex;
  110. }
  111.  
  112. /* This function finds the least frequent letter*/
  113. int findLeast (int letterCount[], int size) {
  114. int count3; /*Count for least frequent*/
  115. int LeastIndex;
  116. for (int i(0); i<size; i++) {
  117. if (count3 > letterCount[i]) {
  118. count3 = letterCount[i];
  119. LeastIndex = i;
  120. }
  121. }
  122. return LeastIndex;
  123. }
  124.  
  125. /* This function finds the second least frequent letter*/
  126. int findNextLeast ( int letterCount[], int size, int LeastIndex) {
  127. int count4; /*Count for 2nd least frequent*/
  128. int SecondLeastIndex;
  129. for (int i(0); i<size; i++) {
  130. if (count4 > letterCount[i] && i != LeastIndex) {
  131. count4 = letterCount[i];
  132. SecondLeastIndex = i;
  133. }
  134. }
  135. return SecondLeastIndex;
  136. }
Add Comment
Please, Sign In to add comment