Trouble passing char/pointer array

This is a discussion on Trouble passing char/pointer array within the C++ Programming forums, part of the General Programming Boards category; I am stumped on a particular problem involing a recursive binary search. Particularly, I am not sure on how to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Trouble passing char/pointer array

    I am stumped on a particular problem involing a recursive binary search. Particularly, I am not sure on how to pass the alotted variables, char/pointer. Every time I try something different I just get more errors. I need some assistance on figuring out how to pass these variables and make them usable. The code I have currently is as follows:

    Code:
    // Binary Search, Recursive Implementation
    
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    int binarySearch(char *names, char nameInput, int leftIndex, int rightIndex){
        while(leftIndex <= rightIndex){
    		int middleIndex = (leftIndex + rightIndex) / 2;
            if (strcmp(searchKey, names[middleIndex]) == 0){
                return middleIndex;
            } else if(strcmp(searchKey, names[middleIndex]) < 0){
                rightIndex = middleIndex - 1;
                binarySearch();
            } else {
                leftIndex = middleIndex + 1;
                binarySearch();
            }
        }
        return -1;
    }
    
    int main() {
    	char *names[] = {"Carl", "George", "Hellen", "John", "Kevin", "Mary",
        "Rachel", "Ralph", "Steve", "Susan", "Timothy", "Zero"};
    	char nameInput[64];
    	int leftIndex=0;
    	int rightIndex=12
    
    	cout << "Name Search Engine" << endl;
    	cout << "Enter Quit to terminate the program" << endl << endl;
    
        while(1) {
    	cout << "Enter a name: ";
            cin.getline(nameInput,64);
    
    	if (strcmp(nameInput,"Quit") == 0) {
    		cout << "End of program" << endl;
    		break;
    	} else if(strcmp(nameInput,names[5])) {
    		cout << "Mary is found at index 5" << endl << endl;
    	}
    	binarySearch(*names, nameInput, leftIndex, rightIndex);
        }
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    what kind of errors are you seeing?


    Code:
    int binarySearch(char *names, char nameInput, int leftIndex, int rightIndex){
    2nd parameter should be:

    Code:
    char *nameInput
    Last edited by HyperShadow; 10-04-2007 at 10:57 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your initial call to binarysearch shouldn't take *names, but names - and consequently, you need char **names in your binarysearch function.

    You also must pass all the arguments to binarySearch here:
    Code:
            if (strcmp(searchKey, names[middleIndex]) == 0){
                return middleIndex;
            } else if(strcmp(searchKey, names[middleIndex]) < 0){
                rightIndex = middleIndex - 1;
                binarySearch();
            } else {
                leftIndex = middleIndex + 1;
                binarySearch();
            }
    And, for efficency, since strcmp() can be a quite "expensive" function, why not call it just the once, and use the result twice. Something like this:
    Code:
            int cmp = strcmp(searchKey, names[middleIndex]);
            if (cmp == 0){
                return middleIndex;
            } else if(cmp < 0){
    ...
    Edit: Also, I suppose "searchKey" should actually be nameInput in all of the binarysearch function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21