C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 24th, 2004, 02:19 PM
grid grid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 grid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
binary search help

Since I'm new in C++ I need some help with this program that is to write a telephone lookup programs. Read a data set of 1000 names and telephone numbers from a file that contains the numbers in random order. Handle lookups by name and also reverse lookups by phone number. Use binary search for both lookups. So far I have only this. Can I get a little help on that


#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;



void findNameAndPrint(string nameToFind, string names[1000],
int phoneNumbers[1000], int numberRead);
int readNamesAndNumbers( string names[1000], int numbers[1000]);

int main(void) {
int phoneNumbers[1000];
string names[1000];
int numberRead;
string nameToFind;

numberRead = readNamesAndNumbers(names, phoneNumbers);

cout << "Input the name to lookup ";
cin >> nameToFind;

findNameAndPrint(nameToFind, names, phoneNumbers, numberRead);

return EXIT_SUCCESS;

}


int readNamesAndNumbers ( string names[1000], int numbers[1000])
{

int numberRead = 0; // Initially no values have been read.

ifstream phoneFile;

phoneFile.open("HWData.txt");

while (phoneFile.good()) {
phoneFile >> names[numberRead] >> numbers[numberRead];
if (phoneFile.good()) {
numberRead++;
}
}

return numberRead;
}

void findNameAndPrint(string nameToFind, string names[1000],
int phoneNumbers[1000], int numberRead)
{
int current;

for (current = 0; current < numberRead; current++) {
if (names[current] == nameToFind) {
cout << names[current] << " " << phoneNumbers[current] << endl;
}
}
return;
}

Reply With Quote
  #2  
Old July 25th, 2004, 06:17 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
I wrote a binary search for someone a while back. Search through the history of this forum and you should find code to do it.

-KM-

Reply With Quote
  #3  
Old July 29th, 2004, 06:45 PM
astralvoid astralvoid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 9 astralvoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 42 sec
Reputation Power: 0
In order to do a true binary search on a list of randomly ordered data, you would need to dynamically sort the data first.

The concept of a binary search is that you start somewhere in the 'middle', check if your search value is higher or lower than the the current position and then jump to half way between that 'correct' half of the data and repeat until you narrow it down to a match.

My suggestion would be to write a function to sort the data dynamically into an array(in effect, create an index), then perform a standard binary search on it.

Hope this helps!

Reply With Quote
  #4  
Old August 10th, 2004, 01:22 AM
arash_partow arash_partow is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 5 arash_partow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello,

Because you have to do a search on both the name and the telephone
number this means you are using 2 different types of indexes.

What you should do is make an index (ie: array of type) for both the
telephone numbers and the names, the index element in each would be
a pair containing the key (ie: tel number or name) and a position
pointing to the 3rd array where both the telephone and names are
stored. The 3rd array does not need to be sorted however both index
arrays should be sorted according to the keys (ie: tel number or name).

struct
{
char* telephone;
unsigned int position;
} TelephoneElement

struct
{
char* name;
unsigned int position;
} NameElement

struct
{
char* name;
unsigned int position;
} CombinedElement


The indexes would like like this:

TelephoneElement* telephoneIndex = new TelephoneElement[NUMBER_OF_ELEMENTS];
NameElementElement* nameIndex = new NameElement[NUMBER_OF_ELEMENTS];
CombinedElement* mainIndex = new CombinedElement[NUMBER_OF_ELEMENTS];


[.] populate all 3 indexes accordingly.
[.] sort the telephoneIndex and the nameIndex


Now you have the necessary infrastructure to perform a binary search on either
the name or the telephone number.


Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > binary search help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT