Question

Combinations of Recursion there is a vector<vector<string> > v = v[0] = { Blue} v[1] =...

Combinations of Recursion

there is a vector<vector<string> > v =

v[0] = { Blue}

v[1] = { Pig, Dog }

v[2] = { jump }

v[3] = { low, high}

________________________________

the cout result will be like (a sentence must have v[1] and v[2], but v[0] and v[3] is not required in the sentence)

Pig jump

Pig jump low

Pig jump high

Dog jump

Dog jump low

Dog jump high

Blue Pig jump

Blue Pig jump low

Blue Pig jump high

Blue Dog jump

Blue Dog jump low

Blue Dog jump high

__________________________

Please use this function to do the recursion:

void recur_sentence(vector<vector<string> >& elements, int position)

{

}

***position means where you are. (at v[0], v[1], v[2] or v[3])

**************Please do this in recursion!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have implemented the recursive function as requested in the question

Below is the C++ code and i have commented the codes to explain its functionality

*******************************C++ CODE***********************************

#include <stdio.h>
#include <iostream>
#include <algorithm>   
#include <vector>
using namespace std;

//Global variable declared
vector<string> fullList;
int loopCount = 1;

//Recursive function to build the string combinations
void recur_sentence(vector<vector<string>> &elements, int position, string strSoFar)
{
//If the position is 2 that means two words formed
if (position == 2)
{
//Check the word combination already added in word list
bool notFound = true;
for (int i=0; i<fullList.size(); i++)
{
if(fullList[i] == strSoFar)
{
notFound = false;
}
}
//If word combination not exist then add to list
if(notFound)
fullList.push_back(strSoFar);
}
//If the position is 3 that means three words formed
if (position == 3)
{
//Check the word combination already added in word list
bool notFound = true;
for (int i=0; i<fullList.size(); i++)
{
if(fullList[i] == strSoFar)
{
notFound = false;
}
}
//If word combination not exist then add to list
if(notFound)
fullList.push_back(strSoFar);
}
//If the position is 4 that means four words formed
if (position >= elements.size())
{
//Check the word combination already added in word list
bool notFound = true;
for (int i=0; i<fullList.size(); i++)
{
if(fullList[i] == strSoFar)
{
notFound = false;
}
}
//If word combination not exist then add to list
if(notFound)
fullList.push_back(strSoFar);
  
//If four word combination formed then call the recursive function
if(loopCount >= 4 && elements.size() == 4)
{
//Remove one element from list
elements.erase(elements.begin());
recur_sentence(elements, 0, "");
}
loopCount = loopCount + 1;
return;
}
//Form loop of the elements vector
for (int i=0; i<elements[position].size(); i++)
{
recur_sentence(elements, position+1, strSoFar+ " " + elements[position][i]);
}
};

int main()
{
//Vector declared
vector<vector<string>> v(4);
v[0] = { "Blue" };
v[1] = { "Pig", "Dog" };
v[2] = { "jump" };
v[3] = { "low", "high" };
  
//Recursive function called
recur_sentence(v, 0, "");
  
//Print the data of string formed from recursive function
for (int i=0; i<fullList.size(); i++)
{
cout<<fullList[i]<<endl;;
}
  
return 0;
}

********************************END*********************************

#include <stdio.h> #include <iostream» #include <algorithm» #include <vector» using namespace std; //GLobal variable declaredfor (int i-0; iful!List . size(); i++) if(fulllist[i]strSoFar) notFound false; //If word combination not exist then add to li

Output Screen

Blue Pig Blue Pig jump Blue Pig jump low Blue Pig jump high Blue Dog Blue Dog jump Blue Dog jump low Blue Dog jump high Pig j

Add a comment
Know the answer?
Add Answer to:
Combinations of Recursion there is a vector<vector<string> > v = v[0] = { Blue} v[1] =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string>...

    *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") { str.erase(str.find_last_not_of(chars) + 1); } vector<string> *get_words() { vector<string> *words = new vector<string>(); fstream infile; string word; infile.open("words.txt"); getline(infile, word); while(infile) { rtrim(word);     words->push_back(word);     getline(infile, word); } return words; } void example_1(vector<string> *words) { // find words that contain the substring 'pet' and 'cat' // HINT: use find(str, p) method....

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT