Write a function method1(v,w) which takes as input two vectors v and w and retur
ID: 3720428 • Letter: W
Question
Write a function method1(v,w) which takes as input two vectors v and w and returns true if every element in v is in w and returns false otherwise. Use the following pseudocode as your template. Make sure you are not copying and pasting any code from findelt1. You should be calling findelt1 directly within this function.
3. Write a function method1 (v,w) which takes as input two vectors v and w and returns true if every element in v is in w and returns false otherwise. Use the following pseudocode as your template. Make sure you are not copying and pasting any code from findelt1. You should be calling findelt1 directly within this function. default output is true for each element a in v % use finde lt 1 that you wrote above if a is not in w, return falseExplanation / Answer
//As the languages are not specified
//Hence Assuming the vector is of char and language required is cpp
//If some special language is required by U ,Plz ask for it in comment
bool find_it(char c,vector<char>v)
{
int l=v.size();
//Traverse over all the elements to check if the given character exists in v
for(int i=0;i<l;i++)
if(c==v[i])
return true;
//If character is not found above in the loop ,then finally return false
return false;
}
bool method1(vector<char>v,vector<char>w)
{
//Declaring the default value as true
bool p=true;
//Now applying the function find it for every element of the vector w
int l=v.size(),k=w.size();
//Iterating over each element of the vector
for(int i=0;i<k;i++)
{
p=find_it(w[i],v);
if(p==true)
continue;
//If p is false,then the element w[i] is not found in vector-v
//Hence return false in this case
//Storing the value in p and returning false
else return p=false;
}
}
***************Another Method **************-------
bool method1(vector<char>v,vector<char>w)
{
//Declaring the default value as true
bool p=true;
int l=v.size(),k=w.size();
map<char,int>m;//By default,the mapped value stored in map is zero(0);
//Iterating over each element of the vector
for(int i=0;i<k;i++)
{
m[w[i]]=1;
}
//Now iterate over all the characters of V,
//If any of the character is found in v,which was not in w,then its map value would be 0;
for(int i=0;i<l;i++)
{
if(m[v[i]]==0) //This charcter does not exist in w;
return false;
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.