Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- int searchSong(std::string playList[], int arraySize, std::string songTitle)
- {
- for (int i = 0; i < arraySize; i++)
- {
- std::cout << ". " << playList[i] << std::endl;
- }
- int start = 0;
- int stop = arraySize - 1;
- int pos1 = start + (stop - start + 1) / 3;
- int pos2 = start + 2 * (stop - start + 1) / 3;
- int songIndex = -1;
- while (start < stop && songIndex == -1)
- {
- if ((playList[start] <= songTitle) && (songTitle < playList[pos1]))
- {
- for (int i = start; i < stop; i++)
- {
- if (playList[i] == songTitle)
- {
- songIndex = i;
- return songIndex;
- }
- }
- }
- else if ((playList[pos1] <= songTitle) && (songTitle < playList[pos2]))
- {
- for (int i = start; i < stop; i++)
- {
- if (playList[i] == songTitle)
- {
- songIndex = i;
- return songIndex;
- }
- }
- }
- else if ((playList[pos2] <= songTitle) && (songTitle < playList[stop]))
- {
- for (int i = start; i < stop; i++)
- {
- if (playList[i] == songTitle)
- {
- songIndex = i;
- return songIndex;
- }
- }
- }
- return songIndex;
- }
- }
- int main()
- {
- char ans;
- do {
- std::string playList[12] = { "addicted", "battery", "cold", "demons", "epic", "everlong", "fuel", "given", "halo", "immortals", "jaded", "jars" };
- int arraySize = sizeof(playList) / sizeof(std::string);
- std::string songTitle;
- std::cout << "Enter a song title: \n";
- std::getline(std::cin, songTitle);
- std::cout << std::endl;
- std::cout << "Searching for: " << songTitle << std::endl << std::endl;
- int songIndex = searchSong(playList, arraySize, songTitle);
- std::cout << std::endl;
- std::cout << "The array index for " << songTitle << " is " << songIndex << std::endl << std::endl;
- std::cout << "Would you like to search again? \n";
- std::cin >> ans;
- std::cin.ignore();
- } while ((ans == 'y') || (ans == 'Y'));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement