Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you help me: _change to binary search on this code. _change number of limbs is random between 0, 2, 4, 6, 8 only. _add
Can you help me:
_change to binary search on this code.
_change number of limbs is random between 0, 2, 4, 6, 8 only.
_add kind of pet
// A Pet Class and its driver #include#include #include #include using namespace std; // Pet class with a name, id, numLimbs. // Also has a static population member. template std::string NumberToString ( T Number ) { std::ostringstream ss; ss << Number; return ss.str(); } class Pet { private: string name; long id; int numLimbs; // static int population = 0; public: // Ctrs Pet() { this->name = ""; this->id = 0; this->numLimbs = 0; } Pet(string name, long id, int numLimbs) { this->name = name; this->id = id; this->numLimbs = numLimbs; } // Getters or Accessors long getID() { return this->id; } string getName() { return this->name; } int getNumLimbs() { return this->numLimbs; } // Setters or Mutators bool setID(long id) { this->id = id; } bool setName(string name) { this->name = name; } bool setNumLimbs(int numLimbs) { this->numLimbs = numLimbs; } // Serialization string to_string() { string s; s.append("Pet: "); s.append(this->name); s.append(". ID: "); s.append(NumberToString(this->id)); s.append("; number of Limbs: "); s.append(NumberToString(this->numLimbs)); return s; } // E.g. Returns "Pet: ufomo. ID: 1024; number of Limbs: 8" }; // Global function to generate a cool sounding name by picking // consonants and vowels in alternate fashion. string makeCoolName(int len) { string coolName; string vowels = "aeiou"; string consonants = "bcdfghjklmnpqrstvwxyz"; for (int i = 0; i < len; ++i) { if(i%2==0) coolName.push_back(vowels[rand()%5]); else coolName.push_back(consonants[rand()%21]); } return coolName; } // In this main driver, we initialize an array of STORE_SIZE Pets // Assign them random values, and enter a user interaction loop // in which the user types in a desired ID, and the system will // search the store for a pet with the ID and print its details. int main() { const int STORE_SIZE = 10*1000; // Declare an array of pets Pet pets[STORE_SIZE]; // Loop over the pets to initialize each Pet for (int i = 0; i < STORE_SIZE; ++i) { pets[i].setID(i+1); pets[i].setName(makeCoolName(5)); pets[i].setNumLimbs(4); } // Enter a user interaction loop in which we while(true) { // - prompt the user for an ID long l; cout << "Enter ID:" << endl; cin >> l; // - If the user entered 0 or a negative number in his/her ID search, exit gracefully. if (l<=0) { cout << "Invalid ID" << endl; break; } // - Scan the array of pets (my pet store) for a Pet with that ID int check = 0; for (int i = 0; i < STORE_SIZE; ++i) { // - If found, print the details of the pet if (pets[i].getID() == l) { cout << pets[i].to_string() << endl; check = 1; break; } } // - If not found, say "No such pet exists in this store" or equiv. message if (check == 0) { cout << "No such pet exists in this store" << endl; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started