alansam

Untitled

Apr 11th, 2021
863
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. int searchSong(std::string playList[], int arraySize, std::string songTitle)
  4. {
  5.     for (int i = 0; i < arraySize; i++)
  6.     {
  7.         std::cout << ". " << playList[i] << std::endl;
  8.     }
  9.     int start = 0;
  10.     int stop = arraySize - 1;
  11.     int pos1 = start + (stop - start + 1) / 3;
  12.     int pos2 = start + 2 * (stop - start + 1) / 3;
  13.     int songIndex = -1;
  14.     while (start < stop && songIndex == -1)
  15.     {
  16.         if ((playList[start] <= songTitle) && (songTitle < playList[pos1]))
  17.         {
  18.             for (int i = start; i < stop; i++)
  19.             {
  20.                 if (playList[i] == songTitle)
  21.                 {
  22.                     songIndex = i;
  23.                     return songIndex;
  24.                 }
  25.             }
  26.         }
  27.         else if ((playList[pos1] <= songTitle) && (songTitle < playList[pos2]))
  28.         {
  29.             for (int i = start; i < stop; i++)
  30.             {
  31.                 if (playList[i] == songTitle)
  32.                 {
  33.                     songIndex = i;
  34.                     return songIndex;
  35.                 }
  36.             }
  37.         }
  38.         else if ((playList[pos2] <= songTitle) && (songTitle < playList[stop]))
  39.         {
  40.             for (int i = start; i < stop; i++)
  41.             {
  42.                 if (playList[i] == songTitle)
  43.                 {
  44.                     songIndex = i;
  45.                     return songIndex;
  46.                 }
  47.             }
  48.         }
  49.         return songIndex;
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     char ans;
  56.     do {
  57.         std::string playList[12] = { "addicted", "battery", "cold", "demons", "epic", "everlong", "fuel", "given", "halo", "immortals",             "jaded", "jars" };
  58.         int arraySize = sizeof(playList) / sizeof(std::string);
  59.         std::string songTitle;
  60.         std::cout << "Enter a song title: \n";
  61.         std::getline(std::cin, songTitle);
  62.         std::cout << std::endl;
  63.         std::cout << "Searching for: " << songTitle << std::endl << std::endl;
  64.         int songIndex = searchSong(playList, arraySize, songTitle);
  65.         std::cout << std::endl;
  66.         std::cout << "The array index for " << songTitle << " is " << songIndex << std::endl << std::endl;
  67.         std::cout << "Would you like to search again? \n";
  68.         std::cin >> ans;
  69.         std::cin.ignore();
  70.     } while ((ans == 'y') || (ans == 'Y'));
  71.     return 0;
  72. }
  73.  
RAW Paste Data