Question

In a single cpp file, provide efficient implementations for the following functions: // Precondition: none //...

In a single cpp file, provide efficient implementations for the following functions:
// Precondition: none
// Postcondition: the integer returned indicates where letter appears in target string,
// with the least significant bit set if letter appears in the last
// position in the string, the second least significant bit set if
// letter appears in the second to last position in the string, etc
int get_mask(string word, char letter);
Example: If word = "abcfab", get_mask(word,'a') should return 34, get_mask(word,'e') should return 0, and get_mask(word,'f') should return 4.
// Precondition: word and mask are of the same length, and mask either has
// the same contents as word, or a '?' where it differs from word
// Postcondition: wherever letter appears in word, the '?' in mask will be updated
// with letter
void update_mask(string word, string& mask, char letter);
Example: If word = "abcfab", mask = "??c???", after the call update_mask(word, mask,'a'), mask should equal "a?c?a?".
Note: update_mask must make a call to get_mask in order to determine where letter appears. It should not scan the string word directly!

// Precondition: word and mask are of the same length
// Postcondition: return true if word and mask have the same contents, and false otherwise
bool guessed(string word, string mask);
Write a main method that contains a robust set of tests and uses assert for each test.
Enforce the preconditions with assert statements.

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

Code:

#include<iostream>
using namespace std;

int get_mask(string word,char letter){
   int cnt = 0;
   for(int i=0;i<word.length();i++){
       if(word[i] == letter){
           //Using the bitwise OR operator
           //to form the binary number
           //The power at index i will be 2*(len-i-1)
           cnt = cnt | (1 << (word.length() - i - 1));
       }
   }
   return cnt;
}

void update_mask(string word,string& mask,char letter){
   //Return if the lengths are not equal
   if(word.length() != mask.length())
       return;
   int position = get_mask(word,letter);
   if(position == 0)
       return;
  
   int idx = mask.length() - 1;
      
   while(position!=0){
       //If the current bit is 1, then if it is ? in the mask string
       //then we can insert the letter at that index.
       if(position & 1){
           if(mask[idx] == '?')
               mask[idx] = letter;
       }
       idx--;
       position = position >> 1;
   }
}

bool guessed(string word,string mask){
   if(word.length() != mask.length())
       return false;
   //If all the corresponding letters are same, then return true
   //else return false  
   for(int i=0;i<word.length();i++){
       if(word[i] != mask[i])
           return false;
   }
   return true;
}

int main(){
   string word = "abcfab";
   string mask = "??c???";
   cout<<"get_mask(word,'a') = "<<get_mask(word,'a')<<endl;
   cout<<"get_mask(word,'e') = "<<get_mask(word,'e')<<endl;
   cout<<"get_mask(word,'f') = "<<get_mask(word,'f')<<endl;
   update_mask(word, mask,'a');
   cout<<"update_mask(word, mask,'a') = "<<mask<<endl;
   update_mask(word, mask,'e');
   mask = "??c???";
   cout<<"update_mask(word, mask,'e') = "<<mask<<endl;
}

Output:

Please appreciate the solution if you find it helpful.

If you have any doubts in the solution, feel free to ask me in the comment section.

Add a comment
Know the answer?
Add Answer to:
In a single cpp file, provide efficient implementations for the following functions: // Precondition: none //...
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
  • Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....

    Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • Create a hangman program. Sample output from your program should look like the following: Current Status...

    Create a hangman program. Sample output from your program should look like the following: Current Status for userInputs= _ _ _ _ _ _ Enter next letter: a Current Status for userInputs=a _ _ _ _ _ _ Enter next letter: e Current Status for userInputs=ae _ _ _ _ e _ Enter next letter: i Current Status for userInputs=aei _ _ _ _ e _ Enter next letter: o Current Status for userInputs=aeio _ o _ _ e _...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

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