- #1
iamjon.smith
- 117
- 3
Homework Statement
This is the assignment instructions:[/B]
- In C++, code a search algorithm that searches a list of strings for a particular song. The searching algorithm will have two inputs: the playlist, which is a string array that contains a list of songs in alphabetical order; and a particular song, which is a string. If the song is found in the list, the algorithm will return the index of the song, and it will return -1 otherwise.
- This searching algorithm will employ a divide-and-conquer approach similar to that in binary search, but with a slight variation. In binary search, a list is split in 2 sublists during each step; however, for your assignment, you will build and algorithm that splits the list into 3 sublists during each step.
Homework Equations
I am VERY VERY new to C++. I have been searching online for several hours now and need help splitting the array into 3 sublists and then searching those 3 lists for the user input. In the following code, I initialize the array with 21 songs, take user input for the search string, and then output the search string and the array being searched so the user can verify whether the search string actually does or does not exist in the songArray.
I need help with the next two steps:
step 1:
Split the array into 3 sublists
step 2:
Search the sublists
The Attempt at a Solution
Code:
#include <iostream>
#include <string>
using namespace std;int main(int argc, char** argv) {
string songName;
string songArray[] = {"Against the Wind", "Bohemian Rhapsody", "California Love", "(Don't Fear) The Reaper", "Facade", "Hello", "I Fought the Law", "Iron Man", "King Nothing", "Knocking On Heaven's Door", "Livin' On a Prayer", "Love Story", "Macarena", "No Particular Place to Go", "On Top of the World", "Party Rock Anthem", "Piano Man", "Pink", "She Talks to Angels", "The Twist", "You Give Love a Bad Name"};
cout << "Please Enter A Song Name..." << endl;
getline (cin, songName);
cout << endl;
cout << "Searching for: " + songName << endl << endl;
for (int i = 0; i < sizeof(songArray)/sizeof(string); i++){
cout << songArray[i] << endl;
}
// split array into 3 sublists and search these lists for songName
// if song exists return index,
// else return -1
return 0;
}
Last edited by a moderator: