Advertisement
Guest User

Untitled

a guest
Jan 31st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <iomanip>
  7.  
  8.  
  9.  
  10. void CountValue(int &WordLength, int &CharCount, int FrequencyArray[]); //function prototype
  11.  
  12. int main() {
  13.  
  14. const int MAX_SIZE_OF_ARRAY = 16; //variable declaration
  15. int FrequencyArray[MAX_SIZE_OF_ARRAY] = { 0 };
  16. int CharCount = 0, WordLength = 0, WordCount = 0;
  17. char ch;
  18. float Average = 0;
  19.  
  20. cout << "Enter words(press '0' to display table and perform calculation):" << endl; //first round at receiving input from either the keyboard or a file
  21. cin.get(ch);
  22.  
  23. while (ch != '0') { //if the input is zero, it will exit the while loop and move to display the table
  24.  
  25. if (isalpha(ch)) //checks to see if whatever is stored in ch is alphabetical and increments wordlength if it is
  26. WordLength++;
  27.  
  28. else {
  29. CountValue(WordLength, CharCount, FrequencyArray);
  30. WordLength = 0;
  31. }
  32.  
  33. cin.get(ch);
  34.  
  35. }
  36.  
  37. while ((ch != '1')) {
  38. cout << "Word Length Frequency" << endl;
  39.  
  40. for (int i = 1; i < MAX_SIZE_OF_ARRAY; i++) {
  41. cout << setw(6) << right << i << " "
  42. << setw(8) << right << FrequencyArray[i] << endl;
  43. }
  44. cout << "Enter 1 to exit the program." << endl;
  45. cin.get(ch);
  46.  
  47.  
  48. for (int i = 1; i < MAX_SIZE_OF_ARRAY; i++) {
  49. WordCount = WordCount + FrequencyArray[i];
  50. }
  51.  
  52. Average = ((float)CharCount) / ((float)WordCount);
  53. cout << "The average word length is: " << Average << endl;
  54.  
  55.  
  56. }
  57. return 1;
  58.  
  59. }
  60.  
  61.  
  62. void CountValue( int &WordLength, int &CharCount, int FrequencyArray[]) {
  63. const int MAX = 16;
  64.  
  65. if (WordLength > 0) {
  66. if (WordLength >= (MAX))
  67. WordLength = MAX - 1;
  68. }
  69. FrequencyArray[WordLength]++;
  70. CharCount = CharCount + WordLength;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement