- #1
richworldinc
- 5
- 1
- Homework Statement
- 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.
- Relevant Equations
- I took advice from another thread and tried to redo this project, but seem to be getting an error that: C4700 uninitialized local variable 'songIndex' used songs. i do not see why this produces this error
#include <iostream>
#include <string>
using namespace std;
int mySearch(string songArray[], int arraySize, string songTitle, int songIndex)
{
for (int i = 0; i < arraySize; i++)
{
cout << i << "..." << songArray << endl;
}
int searchBegin = 0;
int searchEnd = 13;
int pos1 = searchBegin + (searchEnd - searchBegin + 1) / 3;
int pos2 = searchBegin + 2 * (searchEnd - searchBegin + 1) / 3;
songIndex = -1;
while (searchBegin < searchEnd && songIndex == -1)
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
else if ((songArray[pos1] <= songTitle) && (songTitle < songArray[pos2]))
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
}
else if ((songArray[pos2] <= songTitle) && (songTitle < songArray[arraySize]))
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
}
else
{
songIndex = -1;
return songIndex;
}
}
}
int main(int argo, char** argv)
{
char ans;
do
{
string songArray[] = { "i can only imagine", "healing rain", "gods not dead", "while I am waiting", "broken", "need you now", "im not perfect", "lead me", "who you say i am", "i surrender", "10,000 reasons", "here i am to worship", "nobody" };
int arraySize = sizeof(songArray) / sizeof(string);
string songTitle;
cout << "enter a song title: " << endl;
getline(cin, songTitle);
cout << endl;
cout << "locating: " + songTitle << endl;
int songIndex = mySearch(songArray, arraySize, songTitle, songIndex);
cout << endl;
cout << "the index for" << songTitle << " is " << songIndex << endl;
cout << "would you like to search for another song (y/n)?" << endl;
cout << "you must type 'y' or 'n'!";
cin >> ans;
cin.ignore();
} while (ans == 'y');
}
#include <string>
using namespace std;
int mySearch(string songArray[], int arraySize, string songTitle, int songIndex)
{
for (int i = 0; i < arraySize; i++)
{
cout << i << "..." << songArray << endl;
}
int searchBegin = 0;
int searchEnd = 13;
int pos1 = searchBegin + (searchEnd - searchBegin + 1) / 3;
int pos2 = searchBegin + 2 * (searchEnd - searchBegin + 1) / 3;
songIndex = -1;
while (searchBegin < searchEnd && songIndex == -1)
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
else if ((songArray[pos1] <= songTitle) && (songTitle < songArray[pos2]))
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
}
else if ((songArray[pos2] <= songTitle) && (songTitle < songArray[arraySize]))
{
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
{
for (int i = searchBegin; i < searchEnd; i++)
{
if (songArray == songTitle)
{
songIndex = i;
return songIndex;
}
}
}
}
else
{
songIndex = -1;
return songIndex;
}
}
}
int main(int argo, char** argv)
{
char ans;
do
{
string songArray[] = { "i can only imagine", "healing rain", "gods not dead", "while I am waiting", "broken", "need you now", "im not perfect", "lead me", "who you say i am", "i surrender", "10,000 reasons", "here i am to worship", "nobody" };
int arraySize = sizeof(songArray) / sizeof(string);
string songTitle;
cout << "enter a song title: " << endl;
getline(cin, songTitle);
cout << endl;
cout << "locating: " + songTitle << endl;
int songIndex = mySearch(songArray, arraySize, songTitle, songIndex);
cout << endl;
cout << "the index for" << songTitle << " is " << songIndex << endl;
cout << "would you like to search for another song (y/n)?" << endl;
cout << "you must type 'y' or 'n'!";
cin >> ans;
cin.ignore();
} while (ans == 'y');
}