Binary Search/Sort Help

reate a program to do the following:

1) Read in names to sort until the user types the “enter” key as the first character of a string (the maximum number of names is 20, there is not a maximum length to a name).
2) After sorting and displaying the array of strings, ask the user for a string to search for (again, no maximum length).
3) Do a binary search to find if the string is in the array
4) If the string is in the array, tell the user the index of the string.
5) If the string is not in the array, tell the user that it is missing.
6) Repeat items 3 thru 5 until the user enters the “enter” key as the first character of the string, at which time the program will complete.

BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled.


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
#include <iostream>
#include "BinarySearch.h"
#include "Bubblesort.h"
#include <string>


using namespace std;



void main()
{
	int Num;
	char ** Names2;
	Names2 = new char *[20];
	char i;
	void inputStrings(string words[], int Num);


	cout << "How many strings would you like to enter?" << endl;
	cin >> Num;
	cout << "You would like to enter " << Num << "strings. ";

	string words = new char[Num];

	//inputStrings(words, Num);
	for (int i = 0; i < Num; i++)
	{
		cout << "You have entered: " << words[i] << "/n";
		//cin >> Names2[i];
	}
	

		//cout << "Here are the numbers you entered:" << endl;
		//for (int i = 0; i < Num; i++)
		//{
		//	cout << Names2[i] << " ";
		//}
		//cout << endl << endl;

		BubbleSort(Names2[i]);


		cout << "Here are your sorted numbers:" << endl;
		for (int i = 0; i < Num; i++)
		{
			cout << Names2[i] << " ";
		}
		cout << endl;
	}
BinarySearch (N)


[/code]
#include "BinarySearch.h"
#include <string.h> // for string length

int Search(const char Array[], char Item)
{
int First;
int Middle;
int Last;

First = 0;
Last = strlen (Array) -1 ;
do {
Middle = (First + Last) / 2;
if (Array[Middle] == Item)
return Middle;
else
if (Array[Middle] > Item)
Last = Middle - 1;
else
First = Middle + 1;
} while (Last >= First);
return -1;





return Last;
}
[/code]

I believe my Binary Sort is ok, but im lost in the main file. I've missed some classes while healing so im trying to get caught back up. Can someone just point suggestions on what needs to be changed or added for this to work? Much Thanks
Err...no. You are missing the names to be sorted by the bubble sort and your search is searching for a single character not a name.

Search should be declared with the following header:
int Search(const char *names[], const int size, const char* name);

You need to pass in the size of the array as well so that the binary search knows the size of the search space

And your bubble sort...it looks like you are giving it just a single name to sort. Your bubble sort should be declared as follows:
void BubbleSort(const char* student_names[], const int size);

And you call it like this:
BubbleSort(Names2, 20);

You also missed the part where you read the names into an array. That part should have a for-loop that loops 20 times; And each time it loops, you read a name from your source and place that name in the array by indexing into it
--------------------------------------------------------------------------------------------------------------------------------------

IMO, you should be using std::string to store each name rather than char *. C++ is an improvement over C that tries to save you from shooting yourself in the foot with a rope, by giving you data structures that handle memory allocations for you. This corresponds to the hint that says there is not a maximum length to a name

I would also go as far as to suggest that you store the names in an std::vector rather than char **. Vector will automatically resize itself as the array grows (hint for those bonus points)

Finally, why not make use of std::sort, afterall the requirements didn't mention you needed to create your own sorting algorithm? Or you didn't include that part in this post?
Last edited on
Topic archived. No new replies allowed.