-
December 8th, 2010, 09:07 PM
#1
C++, sort names using linear and binary searches?
I'm trying to get used to using the search functions, as their main focus is to just search for something specific in the array. I'm used to looking through numbers, since they only use int. But for this certain problem I'm trying to get, which states:
Create 2 search functions that search through an array of strings (read this from a file)
-A linear search
-A binary search
Pass an integer by reference which keeps track of how many comparisons each function performs for 3 runs each
-5 items
-10 items
-20 items
Resetting the value in between each set of calls
Print these numbers to the screen
I'm not sure basically how to search a text file, for in this case is a list of names, and to copy them into a array so the search functions can do their job. For the rest of the problem, I don't get what they're saying and would like context on what the book I'm learning this from can help me? Examples would be nice. This is the program so far.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int searchList(string [], int, string);
int main()
{
int SIZE1;
char names[20][20];
char temp[20];
int x = 0;
int results1;
string line;
ifstream myfile ("names.txt");
if (myfile.is_open())
{
int x=0;
while (myfile.good() )
{
for(int i = 0; i < 20; i++)
{
myfile.getline(temp,50);
strcpy_s(names[i],temp);
cout << line << endl;
}
}
myfile.close();
results1 = searchList(names, SIZE1, "Conrad");
}
system("pause");
return 0;
}
int searchList(string list[], int numElems, string value)
{
int index = 0;
int position = -1;
bool found = false;
while (index < numElems && !found)
{
if (list[index] == value)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarySearch(int array[], int numElems, int value)
{
int first = 0,
last = numElems - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last)/2;
if (array[middle] == value)
{
found = true;
position = middle;
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Last edited by cilu; December 9th, 2010 at 03:09 AM.
Reason: code tags
-
December 8th, 2010, 10:06 PM
#2
Re: C++, sort names using linear and binary searches?
Your life will be much easier if you use std::strings rather than char arrays, because then the comparison operations are identical to what you'd do for integers. If you use char arrays, not only do you have the safety problems inherent in fixed-size string buffers, but you also need to use strcmp() to do your comparisons rather than just < or >.
-
December 12th, 2010, 02:23 PM
#3
Re: C++, sort names using linear and binary searches?
i keep getting this error C2664 for the line "results1 = searchList(names, SIZE1, "Conrad");" and i keep changing alot in the program and idk what's up with that. Thanks for the reply though
-
December 12th, 2010, 08:21 PM
#4
Re: C++, sort names using linear and binary searches?
Your types do not match
Code:
int searchList(string [], int, string);
//
char names[20][20];
results1 = searchList(names, SIZE1, "Conrad");
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|