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

Using C++ Stacks, Hash Table and Maps Create the following functions: The first

ID: 3910936 • Letter: U

Question

Using C++ Stacks, Hash Table and Maps

Create the following functions:

The first method takes an array of strings and the length of that array and creates a map with the key - String Length and value is pushed into a Stack of strings.

This time the strings can be of same length. It returns the map created with all lengths as keys and all strings in stacks of the respective keys. You have to use the Stack implementation of Standard Template Library.

Ex.

Input - ["Hello", "Aman", "Preet"] , 3

Output - [{4:"Aman"}, {5:("Hello", "Preet") }]

The second method takes a map and a string and checks if the given string is present in the map you created.

Ex. Input - [{4:"Aman"}, {5:("Hello", "Preet") }], "Hello"]

Output- 1

Input - Contains strings that are used to create map

Output - We will test all strings by hashing different lengths and then we also test your isPresent function

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:

Sample Input 3:

Sample Output 3: (ascending order)

Explanation / Answer

I have answered a previous version of this question, so I’m also attaching the modified printMap method along with the other two required methods you asked for. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//method to create the required map

map<int,stack<string> > createMap(string array[],int size){

                //defining a map object

                map<int,stack<string> > myMap;

                //looping through all elements in the array

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

                                //finding string length

                                int len=array[i].length();

                                //pushing item to the stack at position 'len' on the map

                                myMap[len].push(array[i]);

                }

                //returning the map

                return myMap;

}

//method to check if an item is present in the map

bool isPresent(map<int,stack<string> > myMap, string item){

                map<int,stack<string> >::iterator i=myMap.begin(); //defining an iterator

                //looping through the map

                while(i!=myMap.end()){

                                //getting the stack at current position

                                stack<string> temp=i->second;

                                //looping through the stack and checking if the element exists

                                while(!temp.empty()){

                                                if(temp.top()==item){

                                                                //found

                                                                return true;

                                                }

                                                temp.pop();

                                }

                                ++i;

                }

                //not found

                return false;

}

Here is the complete program for testing the above methods (you don’t have to submit this, this is just for testing).

#include<iostream>

#include<map>

#include<string>

#include<stack>

using namespace std;

//method to create the required map

map<int,stack<string> > createMap(string array[],int size){

                //defining a map object

                map<int,stack<string> > myMap;

                //looping through all elements in the array

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

                                //finding string length

                                int len=array[i].length();

                                //pushing item to the stack at position 'len' on the map

                                myMap[len].push(array[i]);

                }

                //returning the map

                return myMap;

}

//method to check if an item is present in the map

bool isPresent(map<int,stack<string> > myMap, string item){

                map<int,stack<string> >::iterator i=myMap.begin(); //defining an iterator

                //looping through the map

                while(i!=myMap.end()){

                                //getting the stack at current position

                                stack<string> temp=i->second;

                                //looping through the stack and checking if the element exists

                                while(!temp.empty()){

                                                if(temp.top()==item){

                                                                //found

                                                                return true;

                                                }

                                                temp.pop();

                                }

                                ++i;

                }

                //not found

                return false;

}

//method to print the map (updated)

void printMap(map<int,stack<string> > myMap){

                //by default, the map contents are sorted by key, so we just have to print it

                map<int,stack<string> >::iterator i; //defining an iterator

                //using iterator, moving through the map

                for ( i = myMap.begin(); i != myMap.end(); i++){

                                //getting the key and value

                                stack<string> s=i->second;

                                int len=i->first;

                                //looping through the stack and printing each of them along the length

                while(!s.empty()){

                                cout<<s.top()<<" "<<len<<endl;

                                s.pop();

                                }

                }

}

int main(){

                //testing the above methods

                string input[]={"Aman","Hello","Preet"};

                map<int,stack<string> > mmap=createMap(input,3);

               

                if(isPresent(mmap,"Hello")){

                                cout<<"Hello is present in map"<<endl;

                }else{

                                cout<<"Hello is not present in map"<<endl;

                }

               

                if(isPresent(mmap,"Rahul")){

                                cout<<"Rahul is present in map"<<endl;

                }else{

                                cout<<"Rahul is not present in map"<<endl;

                }

                printMap(mmap);

}

/*OUTPUT*/

Hello is present in map

Rahul is not present in map

Aman 4

Preet 5

Hello 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote