Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is C++. Show finished code and an example of the output on

ID: 3802377 • Letter: P

Question

Programming language is C++. Show finished code and an example of the output on PuTTY when done. Use g++ -std=c++17 to compile the code on PuTTY.

2. (20 points) Write a program named hw5pr2.cpp which reads words from /usr/share/dict/words the dictionary on build. tamu.edu, into a vector of strings, then counts how many words have a vowel a e ioul in a given position. A sample run should look like this: Which letter position? 137266 words in the dictionary have a vowel D ioul in position 4 Which letter position? 15 3676 words in the dictionary have a vowel Caeiou] in position 15 Note: Be sure to ignore case, that is, count upper or lower case vowels etc.

Explanation / Answer

HashTable data structure can be used to query efficiently.

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <set>

#include <map>

using namespace std;

int main() {

   ifstream fin("/usr/shared/dict/words");

   if (!fin) {

       cout << "Error reading file!!! ";

       return 0;

   }

   vector<string> words;

   string word;

   //read all words from file

   while (fin >> word) {

       words.push_back(word);

   }

   char array[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };

   set<char> S(array, array + 10);

   //build a dictionary to query efficiently

   map<int, int> dictionary;

   for (int i = 0; i < words.size(); ++i) {

       for (int j = 0; j < words[i].length(); ++j) {

           if (S.find(words[i][j]) != S.end()) {

               ++dictionary[j];

           }

       }

   }

   //query until user wants

   char input = 'Y';

   while (input == 'Y' || input == 'y') {

       int position;

       cout << "which letter position? ";

       cin >> position;

       cout << dictionary[position - 1]

               << " word in the dictionary in the word has vowel [aeiou] in its position "

               << position << endl;

       cout << "press y or Y to query more ";

       cin >> input;

   }

   return 0;

}