#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", "pigeon", "pig", "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 num_animals = 127; int search_for(const string wanted, const int minpos, const int maxpos) { // returns the position wanted was found at // or -1 if not found cout << "minpos = " << minpos << ", maxpos = " << maxpos; const int mid = (minpos + maxpos) / 2; const string seen = animals[mid]; cout << ", seen = " << seen << "\n"; if (seen == wanted) return mid; if (seen < wanted) return search_for(wanted, mid + 1, maxpos); if (seen > wanted) return search_for(wanted, minpos, mid - 1); } void main() { cout << "Enter a name: "; const string x = read_string(); const int pos = search_for(x, 0, num_animals-1); if (pos == -1) cout << "NOT FOUND\n"; else { cout << "Found at position " << pos << "\n"; cout << "next animal is " << animals[pos+1] << "\n"; } }