#include "library.h" const string animals[] = { "aardvark", "albatross", "alligator", "amoeba", "ant", "anteater", "antelope", "armadillo", "baboon", "bat", "bear", "bee", "beetle", "butterfly", "caprimulgus", "caribou", "carp", "cat", "caterpillar", "chicken", "chimpanzee", "cod", "cougar", "cow", "crab", "crocodile", "deer", "dingo", "dog", "dolphin", "dove", "duck", "eagle", "eel", "elephant", "elk", "ferret", "flamingo", "flounder", "frog", "gerbil", "giraffe", "gnu", "goat", "goldfish", "gorilla", "haddock", "hamster", "hawk", "hedgehog", "hen", "hippopotamus", "hyaena", "iguana", "jackass", "kangaroo", "koala", "lamprey", "lemur", "leopard", "lion", "llama", "lobster", "monkey", "moose", "mosquito", "moth", "mouse", "nematode", "newt", "octopus", "opossum", "orangutan", "osprey", "otter", "owl", "panda", "panther", "peacock", "penguin", "pheasant", "pig", "pigeon", "porcupine", "prairie-dog", "quagga", "quail", "rabbit", "racoon", "rat", "rhinoceros", "salamander", "sea-urchin", "seahorse", "seal", "sealion", "semicolon", "shark", "shrimp", "skunk", "snake", "sphynx", "spider", "squid", "squirrel", "starfish", "tapeworm", "tiger", "tortoise", "tuna", "turkey", "turtle", "unicorn", "varmint", "vole", "vulture", "walrus", "warthog", "weasel", "weevil", "whale", "woodlouse", "woodpecker", "worm", "yeti", "zebra", "zebu" }; const int size = sizeof(animals) / sizeof(string); // BINARY CHOP SEARCH int find(const string query, const int first, const int last) { if (first > last) return -1; const int middle = (first + last) / 2; const string found = animals[middle]; if (query == found) return middle; if (query < found) return find(query, first, middle - 1); return find(query, middle + 1, last); } void test() { cout << "Type a string: "; const string x = read_string(); const int result = find(x, 0, size - 1); if (result == -1) cout << x << " was not found\n"; else cout << x << " is at position " << result << ": " << animals[result] << "\n"; test(); } void main() { test(); }