vendredi 6 mai 2016

Binary search does not add possible subscript numbers(up to 10) to my int pointer array "possible"

In my function I pass 4 arguments, titles: which is a dynamically allocated 2d cstring array to hold up to 100 words 30 char each + null,temp: which is also a dynamically allocated cstring array which will contain the text the user has entered up to 30 char as well, choices:a dynamically allocated int array of 10 elements and index: which contains the number of words in titles, all of the words in titles are sorted and have the first letter capital, same with the user input. my loop right now is not assigning the possible subscript numbers to possible(up to ten) and only finds up to one result if it is a partial search. the partial search btw for this programs users i want to only really allow first parts of words. return quadratic as an option if they write quad but not if they write dratic.

int fileSearch(char **files, char *toFind, int *possible,int numOfFiles)
{
    //set bounds of the while loop
    int lo=1;
    int hi=numOfFiles;
    int mid=0;
    //to indicate possible choices
    int i=0;

    bool found = false;

    //This logic finds words or parts of words and returns either the one selection
    //or up to 10 possibilities in an allocated array
    while(lo<=hi&&!found&&i<CHOICES_AMOUNT)
    {
        mid=lo+(hi-lo)/2;
        //returns sub num to easily access file quicker for user
        if(strcmp(files[mid],toFind)==0)
        {
            found=true;
            return mid;
        }
        //CHECKS IF USER SEARCH IS LESS THEN THE FILE NAME THEN IF IT CAN FIND THE
        //USER SEARCH IN THE FILE NAME
        else if(strcmp(files[mid],toFind)>0)
        {
            hi=mid-1;

            if(strstr(files[mid],toFind)!=0)
            {
                possible[i]=mid;
                i++;
            }
        }
        else
        {
            lo=mid+1;
        }
    }
    //return the negative of i for possibilities logic listed by function call
    //IF IT IS 4 POSSIBILITIES, I WANT TO RETURN -4
    if(i>0)
        return (i-(2*i));
    else
        return (NOT_FOUND);
}

Aucun commentaire:

Enregistrer un commentaire