Comp Sci Search an array in three subsections for a specified song

  • Thread starter Thread starter richworldinc
  • Start date Start date
  • Tags Tags
    Array Search
AI Thread Summary
The discussion focuses on a C++ program designed to search for a specified song title in an array of song titles. Users encountered issues with uninitialized variables and infinite loops due to incorrect comparisons in the search logic. Suggestions were made to initialize variables properly and to revise the search algorithm to avoid lexicographic errors. The conversation also touched on understanding the time complexity of the search function, emphasizing the need to analyze the number of operations relative to the size of the input array. The overall goal is to refine the search functionality while ensuring accurate results for user queries.
richworldinc
Messages
5
Reaction score
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');
}
 
Physics news on Phys.org
You need to repost you code in "code" tags so the forum software doesn't interpret
Code:
[i]
as an italics tag and strip it.
 
  • Like
Likes berkeman
[CODE lang="cpp" highlight="79"]#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');
}[/CODE]
 
  • Like
Likes berkeman
richworldinc said:
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
To fix this error, declare and initialize songIndex in a separate line.
C++:
int SongIndex = 0;
.
.
.
songIndex = mySearch(songArray, arraySize, songTitle, songIndex);

That's just the start of your problems though, as your code goes into an infinite loop. The logic in your mySearch() function is faulty. Here's one of the reasons why.
C++:
if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
In this comparison you're seeing if a given string in the array of strings is less than or equal to the string you're looking for. The actual comparison being done is a lexicographic comparison; i.e., seeing if the strings are in dictionary order. Since the strings in your array aren't in dictionary order, this comparison isn't going to work. In this sort of comparison, "cat" < "dog" and "cat" < "catch".

The best solution I can think of is to cycle through each subarray to see if a given string is equal to the search string.
 
Code:
#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[i] << endl;
    }

    int searchBegin = 0;
    int searchEnd = 13;
    int pos1 = searchBegin + (searchEnd - searchBegin + 1) / 3;
    int pos2 = searchBegin + 2 * (searchEnd - searchBegin + 1) / 3;

    while (searchBegin < searchEnd && songIndex == -1)
    {
        if ((songArray[searchBegin] <= songTitle) && (songTitle < songArray[pos1]))
        {
            for (int i = searchBegin; i < searchEnd; i++)
            {
                if (songArray[i] == 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[i] == 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[i] == songTitle)
                        {
                            songIndex = i;
                            return songIndex;
                        }
                    }
            }
        }
        else
        {
            songIndex = -1;
            return songIndex;
        }
    }
}
int main(int argo, char** argv)
{
    char ans;
    do
    {
        int songIndex = -1;
        string songArray[] = {"10,000 reasons", "Broken", "God's not Dead", "Healing rain", "Here I am to worship", "I surrender", "I'm not perfect", "Lead me", "Nobody", "While I'm waiting", "Who you say I am"};
        int arraySize = sizeof(songArray) / sizeof(string);
        string songTitle;
        cout << "enter a song title: " << endl;
        getline(cin, songTitle);
        cout << endl;
        cout << "locating: " + songTitle << endl;
        int songPlace = mySearch(songArray, arraySize, songTitle, songIndex);
        cout << endl;
        cout << "the index for" << songTitle << " is " << songPlace << 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');
}

this works as long i write the name exactly as it is in the code(i can deal with that)
now i have having trouble tyring to figure out what lines to use and how to find the time complexity
 
Last edited by a moderator:
richworldinc said:
i have having trouble tyring to figure out what lines to use
What do you mean by "what lines to use"?
 
I am using two int statements
one being int mysearch and the other being int main, both of them have to run to get the user input and compute the answer
I really am completely lost when trying to compute the time algorithm
 
richworldinc said:
I am using two int statements
one being int mysearch and the other being int main, both of them have to run to get the user input and compute the answer
It's hard to tell what you know or don't know, based on what you wrote above. The main() function is the entry point for your program. Aside from the fact that the body of main() repeats in a loop, asking the user to enter a song title, the bulk of the work is done by the mySearch() function. To figure out the time complexity (not the time algorithm), you need to look at what mySearch() is doing.
richworldinc said:
I really am completely lost when trying to compute the time algorithm
This is pretty vague. The best I can do with such limited information is to suggest that you determine how many operations (such as comparisons) need to be done for a list of N song titles. Since you have broken the list of songs into thirds, calculating the time complexity in one part would be helpful. Also, on average (over a large number of searches, the song you're looking for will be somewhere in the middle section. This implies that an average search will have to go through the first third of song titles, and will be found about halfway through the middle third of song titles.

Do you have any worked examples of what is being asked for, like from textbook examples or class notes?
 

Similar threads

Back
Top