Create an h file for binarySearch program

I've created a program that asked the user to enter a name if the name entered then computer says the name is in the array. I'm trying to modify this program so that it uses a driver.h file. Can anyone help me figure out what the h file would look like for this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 #include <iostream>
#include <string>
using namespace std;

// functions
void stringSort(string[], int);
bool binarySearch(const string[], string, int);

int main()
{
    
    const int NUM_NAMES = 20; // number of elements in the array
    string names [NUM_NAMES] = {"Smith, John", "Newsad, Kaitlyn", "Doe, John", "Doe, Jane"}; // the string of names 
    string searchName; // the name the user wants to search for

    //call sorting function on array names
    stringSort(names, NUM_NAMES);

    
   
    // Ask user what name they want to serahc 
    cout << "This program has you enter a person's last and first name. Then see's if name entered is in the array.\n";
    cout << "Enter the name to search (ex: Smith, John):\n";

    // get the input name using getline function
    getline(cin, searchName);

    // call function to binary search input name
    // and display message according to function output
    if (binarySearch(names, searchName, NUM_NAMES))
        cout << searchName << " was found in list!\n"; // if the searchName was found in the array
    else
        cout << searchName << " was not found!\n";  // if the searchName was not found in the array

    return 0;  // return zero;
}

// ---------------------------------------------------------------
// stringSort: Sort the names in ascending order.
// string names []: array of names
// arraySize: the size of the array
// ---------------------------------------------------------------
void stringSort(string names[], int arraySize) {
   

    // Declare variables
    int startArray; // start of the array

    // Store varibales temporary in minIdex and minName
    int minIndex; // min number of the index
    string minName; // min name of the index

    // for loop for all elements until second to last
    for (startArray = 0; startArray < arraySize - 1; startArray++) {

        // initialize minValue and minIndex to first element
        minName = names[startArray];
        minIndex = startArray;

        // for loop on all remaining elements of array
        for (int index = startArray + 1; index < arraySize; index++) {

            // see if current string element is less than current minValue
            if (names[index] < minName) {

                //update minValue and minIndex to values of current element
                minName = names[index];
                minIndex = index;
            }
        } //inner for loop ends here


        // the current [startScan] element to its final position
        names[minIndex] = names[startArray];

        // lowest value first
        names[startArray] = minName;
    }
}


//---------------------------------------------------------------------------
// binarySearch: searches the array names[first] throught names[last].
// const string names[]: the array to search throught
// searchName: the name that was search
// arraySize: the size of the array
// returns false if user name/element number was not found
// --------------------------------------------------------------------------
bool binarySearch(const string names[], string searchName, int arraySize) {
    
    // Declare variables
    int first = 0; // first index of the element 
    int last = arraySize - 1; // last index of the element
    int middle; // middle index of the element 

    // While loop until first and last indexes have not overlapped
    while (first <= last) {
        middle = (first + last) / 2; // get the middle index


        // if middle elemnet equals the searchName then return true
        if (names[middle] == searchName) {
            return true;
        }

        // if middle element is greater than searchName
        // then update the 1st part of the array search
        else if (names[middle] > searchName) {
            last = middle - 1;
        }

        // if middle element is less than searchName
        // update search space to 2nd half of the array
        else if (names[middle] < searchName) {
            first = middle + 1;
        }
    }

    // if loop has finished and the number has not been found, return false
    return false;
} 
there are a few camps on what goes where, but I would put up to line 8 as the .h file
I prefer my cpp file to include ONLY one header, its own .h file and no preprocessor if avoidable.

line 38+ would be a new cpp file if you are splitting it up fully.

while we are camping on style anyway, I like the comment blocks you have on the function bodies to be in the .h file as well, with the prototypes. The general idea is that I want to open the smaller .h file and know all I need to know to use this code, if I am not modifying it but just using it, and I don't need to see the details I just want functions, what they do, what the parameters are.
Last edited on
DON'T use include... in a header file. This WILL cause problems at some point. In a header file ALWAYS use the full name (with all the necessary namespace names etc).

I'm trying to modify this program so that it uses a driver.h file


Why? In any case, the header file would probably be named something like strsort.h - not driver.h which doesn't mean anything from it's name.

IMO, I'd put the whole of binarySearch() and stringSort() into a .hpp file, mark them inline and then include that .hpp file at the beginning of main.

Note that it's better practice to use type size_t for an array size instead of type int as size_t is unsigned.

L47 startArray should be defined as part of the for loop - not outside it.

L50 minIndex should be initialised when defined.

L61 use size_t rather than int.

L92 - 94. These variables could be defined as part of a for loop to replace the while loop on L97. Again they should be type size_t and not int.

In main(), you should distinguish between the maximum size of names array and the actual number of entries. You're saying that names has 20 entries, but you're only using 4. The size passed to stringSort() and binarySearch() should reflect the actual used size, not the max size of the array.



Last edited on
Topic archived. No new replies allowed.