So I need help writing a few functions in C++ using recursion. Some word games,
ID: 3857111 • Letter: S
Question
So I need help writing a few functions in C++ using recursion.
Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics.This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the word ``pot'' is an anagram of the string `"otp." A sample run of the program is given below. Your output does not have to be formatted exactly the same as that shown in the sample, but should be in a similar style.
All repetition must be accomplished using recursion.
Sample Runs
Here are two examples of how the program might work:
I need help writing the following functions:
int readDictionary(istream &dictfile, string dict[]);
Places each string in dictfile into the array dict. Returns the number of words read into dict. This number should not be larger than MAXDICTWORDS since that is the size of the array.
int recursivePermute(string word, const string dict[], int size, string results[]);
Places all the permutations of word, which are found in dict into results. Returns the number of matched words found. This number should not be larger than MAXRESULTS since that is the size of the array. The size is the number of words inside the dict array.
void recurPrint(const string results[], int size);
Prints size number of strings from results. The results can be printed in any order.
For words with double letters you may find that different permutations match the same word in the dictionary. For example, if you find all the permutations of the string kloo using the algorithm we've discussed you may find that the word look is found twice. The o's in kloo take turns in front. Your program should ensure that matches are unique, in other words, the results array returned from the recursivePermute function should have no duplicates. A nice way to test this, and your function in general, might be to use the assert facility from the standard library. If done properly the following code should run without a runtime error being generated.
Again, your solution must not use the keywords while, for, or goto or any STL algorithm functions. You must not use global variables or variables declared with the keyword static, and you must not modify the function parameter lists.You must use the integer constants MAXRESULTS and MAXDICTWORDS, as the declared sizes of your arrays, as in the anagram.cpp example provided to you.
Basically, the dictionary we are using is a word file called words.txt and it holds 30,000 words in it, so MAXDICTWORDS = 30000 and MAXRESULTS = the max number of words that can be formed, aka 20.
This is the main function you can use:
Explanation / Answer
This is the program of ANAGRAM
===================================================================
#define ANAGRAM_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector <string> oldDict;
vector <string> newDict;
int dictSize = 0;
void readDict(void){ // Allows the user to input all the words they would like to have in the anagram dictionary.
cout<< "Please enter the number of dictionary entries you wish to create ";
cin >> dictSize;
string word = "";
cout << "Please enter the dictionary entries, 1 by 1, pushing <enter> after each entry. ";
for(int i = 0; i <dictSize; i++){
cin >> word;
oldDict.push_back(word);
}
newDict = oldDict;
}
void sortChars(void){ //sorts the letters in each word of the 'dictionary' so that the letters are in alphabetical order.
for(int i = 0; i < dictSize; i++){
std::sort(newDict[i].begin(), newDict[i].end());
}
}
void getWords(void)
{
int num = 0;
cout << "Please enter the number of words for which you would like to find anagrams of: ";
cin >> num;
string word = "";
for(int i = 0; i < num; i ++){
cout << "Please enter a word: ";
cin>>word;
std::sort(word.begin(), word.end());
for(int i = 0; i < dictSize; i++){
string word2 = newDict[i];
bool isAn = isAnagram(word, word2);
if(isAn == true){
cout << oldDict[i];
} else{
}
}
}
}
bool isAnagram(string word1, string word2){
if(word1.compare(word2) ==0){
return true;
} else {
return false;
}
}
void menu(void){
readDict();
sortChars();
getWords();
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.