Binary Search Array From A File Help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Binary Search Array From A File Help

 
0
  #1
Mar 5th, 2009
Thanks in advance for your help. The problem is this: I am supposed to read an array from a file and ask the user to input a name to search for within the file. If the name is there then return the position number in the file, if not output an error message. I get my program to read the file but when it searches for the name it always comes back false. What am I missing? Here is my code:

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int binarySearch(string[], int, string);
  6.  
  7. const int SIZE = 20;
  8.  
  9. int main()
  10. {
  11. //define array and input
  12. string names[SIZE];
  13. string peopleSearch;
  14. int results;
  15. char ag;
  16. ifstream dataIn;
  17. dataIn.open("sortedNames.txt");
  18.  
  19. do
  20. {
  21. //get user data
  22. cout << "Enter the name of the person you would like to
  23. search for (Last, First): \n";
  24. cin.ignore();
  25. getline(cin, peopleSearch);
  26. cout << endl;
  27.  
  28. //search for the person
  29. results = binarySearch(names, SIZE, peopleSearch);
  30.  
  31. //return -1 if person not found
  32. if (results = -1)
  33. {
  34. cout << "That person does not exist in this array. You might
  35. try \n";
  36. cout << "www.randyspeoplesearch.com. That is a great site
  37. for finding people.";
  38. cout << endl;
  39. cout << endl;
  40. }
  41. else
  42. {
  43. //Otherwise results contain info
  44. cout << "Congratulations! You have found "
  45. << peopleSearch << " in the array \n";
  46. cout << "in position " << results << "of the array.";
  47. cout << endl;
  48. cout << endl;
  49. }
  50.  
  51. cout << "Would you like to run another person search? Y/N: ";
  52. cin >> ag;
  53. cout << endl;
  54.  
  55. }while (ag == 'y' || ag == 'Y');
  56.  
  57. dataIn.close();
  58.  
  59.  
  60. system("pause");
  61. return 0;
  62. }
  63.  
  64. int binarySearch(string array[], int size, string value)
  65. {
  66. int first,
  67. last = size - 1,
  68. middle,
  69. position = -1;
  70. bool found = false;
  71. string s1, s2;
  72. int index;
  73. int pos;
  74.  
  75. while (!found && first <= last)
  76. {
  77. middle = (first + last) / 2;
  78. if (array[middle] == value)
  79. {
  80. for(int i = 0; i < size; i++)
  81. {
  82. for(int h = 0; h < size; h++)
  83. {
  84. if (s1 == s2)
  85. {
  86. found = true;
  87. position = middle;
  88. }
  89. }
  90. }
  91. }
  92. else if (array[middle] > value)
  93. last = middle - 1;
  94. else
  95. first = middle + 1;
  96. }
  97.  
  98. return position;
  99. }
Last edited by meistrizy; Mar 5th, 2009 at 11:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Binary Search Array From A File Help

 
1
  #2
Mar 6th, 2009
Two questions.
Is the file data in ascending order?

What is that for loop inside the first if of your binary search supposed to be doing? If you found that array[middle] is the value you seek, return middle, right there and then. That for loop just spins around a lot, and since s1 and s2 were never initialized, they probably will match as empty strings.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Binary Search Array From A File Help

 
1
  #3
Mar 6th, 2009
You are not filling names[] from dataIn
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Binary Search Array From A File Help

 
0
  #4
Mar 6th, 2009
jencas - good catch. I was just looking at the sort function.

meistrizy - you need a function that reads the data from the file into your names[] array. It ought to return the number of names actually read in, which should be used when passing the array to the sort, rather than using the size of the array. There might not be 20 names in the file.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Binary Search Array From A File Help

 
0
  #5
Mar 21st, 2009
Well...I modified my program but it still isn't reading the names in properly. It will decide whether or not it is true or false but not based on the input from the file. Since it is not reading the names in right it only spits out the last position. ???

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void selectSort(string[], int);
  6. void binSearch (string[], int);
  7.  
  8. int main()
  9. {
  10. const int size = 20;
  11. string array[size];
  12. int results;
  13. char ag;
  14. string peeps;
  15.  
  16. binSearch(array, size);
  17.  
  18. system ("pause");
  19. return 0;
  20. }
  21.  
  22. void selectSort(string array[], int s)
  23. {
  24. int startScan, minIndex;
  25. string minValue;
  26. string peopleSearch;
  27. ifstream dataIn;
  28. dataIn.open("names.txt");
  29.  
  30. if (!dataIn)
  31. cout << "There was an error opening up this file.";
  32. else
  33. {
  34. for (int i; i < s; i++)
  35. getline(dataIn, array[i]);
  36.  
  37. for (startScan = 0; startScan < (s - 1); startScan++)
  38. {
  39. minIndex = startScan;
  40. minValue = array[startScan];
  41. for (int index = startScan + 1; index < s; index++)
  42. {
  43. if (array[index] < minValue)
  44. {
  45. minValue = array[index];
  46. minIndex = index;
  47. }
  48. }
  49. array[minIndex] = array[startScan];
  50. array[startScan] = minValue;
  51. }
  52. }
  53. dataIn.close();
  54. }
  55.  
  56. void binSearch(string array[], int s)
  57. {
  58. string peeps;
  59. int first = 0,
  60. last = s - 1,
  61. middle,
  62. position = 0;
  63. bool found = false;
  64.  
  65. cout << "Enter a name you would like to search for: \n";
  66. getline(cin, peeps);
  67. cout << endl;
  68.  
  69. selectSort(array, s);
  70.  
  71. while (first <= last && !found)
  72. {
  73. middle = (first + last) / 2;
  74. if (array[middle] == peeps)
  75. {
  76. found = true;
  77. position = middle;
  78. }
  79. else if (array[middle] > peeps)
  80. {
  81. last = middle - 1;
  82. position = last;
  83. }
  84. else
  85. {
  86. first = middle + 1;
  87. position = first;
  88. }
  89. }
  90.  
  91. if (found)
  92. {
  93. cout << endl;
  94. cout << "That person does not exist in this array. You might
  95. try \n";
  96. cout << "www.randyspeoplesearch.com. That is a great site
  97. for finding people.";
  98. cout << endl;
  99. cout << endl;
  100. }
  101. else
  102. {
  103. cout << endl;
  104. cout << "Congratulations! You have found " << peeps << " in
  105. the array \n";
  106. cout << "in position " << position << " of the array.";
  107. cout << endl;
  108. cout << endl;
  109. }
  110. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Binary Search Array From A File Help

 
0
  #6
Mar 22nd, 2009
read and heed your compiler warnings.
This line
  1. else
  2. {
  3. for (int i; i < s; i++) // THIS ONE
  4. getline(dataIn, array[i]);
uses an uninitialized value for i - so where's your data going?

Then, I would suggest you really look at your structure - it doesn't make a lot of sense.
Each function should do one thing, and one thing only. Your binsearch( ) gets the search target, then calls selectsort(), which does the actual program input?! before sorting the data.

Break these out so they are independent of each other. Sort sorts, search searches, input should be a separate function. Let main ask the user what to look for after data has been read and sorted. And don't assume that just because your data array is size 20 that you will always get exactly 20 data values from the file.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Binary Search Array From A File Help

 
0
  #7
Mar 24th, 2009
Thanks for your input and suggestions vmanes. My code works- I found that the problem was in my if/else statement. Results should == -1 indtead of results = -1.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Binary Search Array From A File Help

 
0
  #8
Mar 24th, 2009
Originally Posted by meistrizy View Post
Thanks for your input and suggestions vmanes. My code works- I found that the problem was in my if/else statement. Results should == -1 indtead of results = -1.
Well, there is that problem as well. Glad you got it all going.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Binary Search Array From A File Help

 
0
  #9
Mar 24th, 2009
I did follow your advice and initialize that variable in my for loop, moved the if/else statement to main but left the functions as is. Thanks again. :-)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC