error with using a binary search

Apr 22, 2010 at 8:53pm
The program takes in a list of names, then sorts them, then asks for a name to serach for, and shoujld give the binary address of the memory.

As of right now, it crashes as soon as I type in a name to search for. Here is the 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
#include <iostream>
#include <stdlib.h>

using namespace std;

char first, last, C;

char **Create2DArray (unsigned int NumRows, unsigned int NumCols)
{
	char **	TwoDArray;
	TwoDArray = new char * [NumRows];
	for (unsigned int i = 0; i < NumRows; i++)
		TwoDArray [i] = new char [NumCols];
       return TwoDArray;
}

void Free2DArray (char **TwoDArray, unsigned int NumCols)
{
	for (unsigned int i = 0; i < NumCols; i++)
		delete [] TwoDArray [i];	
	delete [] TwoDArray;		
}

void BubbleSort (char ** Array, char NumofNames = 20)
{
	char i=0, j=0;
	for (i = 0; i < NumofNames; i++)
	{			
		for (j = 0; j < NumofNames-1; j++)
		{
			if (strcmp (Array [j+1], Array [j]) == -1)
			{
				char *temp  = Array [j];
				Array [j]   = Array [j+1];
				Array [j+1] = temp;
			}
		}
	}
}

char BinarySearch (char TwoDArray[10])
	{
	do {
		char mid = (last + first) / 2;  // compute mid point.
        if (TwoDArray[mid] == C)
			return mid;
        else if (C < TwoDArray[mid]) 
           last = mid - 1;
        else
           first = mid+1;     // found it. return position
	   }  while (first <= last);
	return -1;
	}

int main ()
{
    unsigned int NumofNames = 5;
    unsigned int NumCols = 20;
	cout << "Enter your names with a space in between each name. Type the ENTER key when finished. " << endl;
	cout << "\nEnter list of names: ";
    char ** TwoDArray = Create2DArray (NumofNames, NumCols);
    for (unsigned int i = 0; i < NumofNames; i++)
		{
		cin >> TwoDArray [i];
		
		if ((cin.get() == '\n') && (NumofNames <= 5))
			{
			cout << "\nYou have entered: " << endl;
			for (unsigned int j = 0; j < NumofNames; j++)
				cout << TwoDArray [j] << "  " << endl;
				cout << endl;
			}
		}

	BubbleSort (TwoDArray, NumofNames);

	cout << "\nHere are the names in sorted order:\n";
	for (unsigned int k = 0; k < NumofNames; k++)
		cout << TwoDArray [k] << "  " << endl;
		cout << endl;

	cout << "Which name do you want to search for? " << endl;
	for (unsigned int i = 0; i < NumofNames; i++)
		{
		cin >> TwoDArray[i];

		BinarySearch (TwoDArray[10]);
		cout << "The index of the string is: " << TwoDArray << endl;
		}

	return 0;
	Free2DArray (TwoDArray, NumCols);
}
Apr 22, 2010 at 10:15pm
may i suggest u use an vector<string> to put the names in...
in that way u can use eather the sort and find functions from algorithm, or u can make ur own, but it should be much easyer then with dinamic alocation of 2D arrays
Apr 22, 2010 at 10:23pm
We have to use the dynamic array, as set by the "requirements" of our professor for this assignment. At this point, I need the binary search to work, but I am not sure what Name should be compared against since I am not sure how to compare name to another name in my array...

1
2
3
4
5
6
7
8
cout << "Which name do you want to search for? " << endl;
	cin >> Name;

	BinarySearch (TwoDArray[10]);
	if (Name = TwoDArray)
		cout << "The index of the string is: " << Name << endl;
	else
		cout << "The string you seek is missing. " << endl;
Apr 22, 2010 at 10:26pm
first of all, change the if to if(Name==TwoDArray)
and second, try to make just one array that will store the names, and when u search for a name, count how many places the pointer has changed, that counter will give u the index you are searching for.
Topic archived. No new replies allowed.