Reading txt file into Hash Table

Reply

Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Reading txt file into Hash Table

 
0
  #1
Feb 20th, 2005
I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. //function declaration
  7. int HashAddress (int PartNumber, char Algorithm);
  8.  
  9. // global variables
  10. int array[41];
  11.  
  12. int main()
  13. {
  14.  
  15. int i = 0;
  16. int max_read = 41;
  17. int amountRead = 0;
  18. int PartNumber, Index;
  19. char Algorithm;
  20.  
  21. // set array elements to -1
  22. for(i = 0; i < 41; i++)
  23. {
  24.  
  25. array[i] = -1;
  26.  
  27. }
  28.  
  29. std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
  30.  
  31. if(!inputfile)
  32. {
  33.  
  34. std::cout<<"Could not open file"<<std::endl;
  35. return 1;
  36.  
  37. }
  38. // prompt for input from user to choose algorithm
  39.  
  40. cout<<"Please select Algorithm\n";
  41. cout<<"1. Modulo Division using linear probing \n";
  42. cout<<"2. Modulo Division using key offset \n";
  43. cout<<"3. PsuedoRandom using linear probing \n";
  44. cout<<"4. Rotation using linear probing \n";
  45. cin >> Algorithm;
  46.  
  47.  
  48.  
  49. //this is where we are reading in the information into our array
  50. for(i = 0; i < 41; i++)
  51. {
  52.  
  53. // as you read each part number in the file
  54. inputfile >> PartNumber;
  55.  
  56. // HashAddress function returns array index and it is assigned to variable Index
  57. Index = HashAddress(PartNumber,Algorithm);
  58.  
  59. // put part number in array
  60. array[Index] = PartNumber;
  61.  
  62. }
  63.  
  64. return 0;
  65.  
  66. }
  67.  
  68. // function HashAddress returns location for hash table
  69. int HashAddress(int PartNumber, char Algorithm)
  70. {
  71.  
  72. int Index, count;
  73. int x, y, z;
  74.  
  75.  
  76. // use algorithms to hash address
  77. switch(Algorithm)
  78. {
  79. case '1': // first algorithm, Modulo Division with linear probing
  80. Index = PartNumber % 41;
  81.  
  82.  
  83. break;
  84.  
  85. case '2': // second algorithm, Modulo Division with key offset
  86. Index = PartNumber % 41;
  87.  
  88. break;
  89.  
  90. case '3': // third algorithm, Psuedorandom with linear probing
  91. Index = (17 * PartNumber + 7) % 41;
  92.  
  93. break;
  94.  
  95. case '4': // fourth algorithm, Rotation with linear probing
  96. x = PartNumber % 10;
  97. y = PartNumber / 10;
  98. z = x * 10;
  99. Index = (z+y) % 41;
  100.  
  101. break;
  102.  
  103. default:
  104. cout << "Invalid Selection\n"; //invalid choice
  105. }
  106.  
  107. cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index;
  108.  
  109. // check to see if array element is occupied
  110. if(array[Index] < 0)
  111.  
  112. // array element is available so return the address/location
  113. return Index;
  114.  
  115. else //collision, part number is already in that spot
  116. {
  117. count = 0; // use count to stop if table is full
  118.  
  119. do
  120. {
  121.  
  122. switch(Algorithm)
  123. {
  124. case '1':
  125. Index = (Index + 1) % 41; // % 41 allows for wrap around
  126. break;
  127.  
  128. case '2':
  129.  
  130. Index = (PartNumber / 41) % 41;
  131.  
  132. case '3':
  133. Index = (Index + 1) % 41; // % 41 allows for wrap around
  134. break;
  135.  
  136. case '4':
  137. Index = (Index + 1) % 41; // % 41 allows for wrap around
  138. break;
  139.  
  140. default:
  141. cout << "Invalid Selection\n"; //invalid choice
  142. }
  143. count ++;
  144. } while ((array[Index] > 0) && (count < 41));
  145.  
  146.  
  147.  
  148.  
  149. if (count == 41)
  150. {
  151. //print ERROR hash table is full
  152. cout << "Hash table is full. Can not insert part number.";
  153. }
  154. else
  155. cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index;
  156.  
  157. return Index;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Reading txt file into Hash Table

 
0
  #2
Feb 20th, 2005
I am not quite sure what your part.txt file looks like, but you have to tell your for loop when the file ends, or it will just keep going 40 times.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Reading txt file into Hash Table

 
0
  #3
Feb 20th, 2005
The best way to read from a file is to use the input gathering function itself as your loop condition. That way when it returns a failure code (supposedly for reaching end-of-file), you break the loop:
  1. while (inputfile >> PartNumber) {
  2. Index = HashAddress(PartNumber, Algorithm);
  3. array[Index] = PartNumber;
  4. }
I removed your comments because they do not add anything. Comments that say the same thing as the code being commented only serve to make the program harder to read. There's also a good chance that any changes to the code will not be reflected in the comment and the two will disagree. When code and comments disagree, it's customary to see both as incorrect.

After the loop, it's also a good idea to make sure that it was end-of-file that caused it to terminate. That way you can handle real errors:
  1. while (inputfile >> PartNumber) {
  2. Index = HashAddress(PartNumber, Algorithm);
  3. array[Index] = PartNumber;
  4. }
  5.  
  6. if (!inputfile.eof()) {
  7. // Handle a real input error
  8. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Re: Reading txt file into Hash Table

 
0
  #4
Feb 20th, 2005
thanks - I was thinking about it after - and I am only entering 30 part numbers - and the array holds 41 elements...I know its not the best way to do it...but I just changed the value to 30. I think there must be a fancy way to put something in there to know its at the end of the file. But I will worry about that another time.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Reading txt file into Hash Table

 
0
  #5
Feb 20th, 2005
I think there must be a fancy way to put something in there to know its at the end of the file.
There is, but it is broken. As I said before, the best way is to use the return value of your input function as a loop controller.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 72
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: Reading txt file into Hash Table

 
0
  #6
Oct 8th, 2008
Originally Posted by LisaJane View Post
I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. //function declaration
  7. int HashAddress (int PartNumber, char Algorithm);
  8.  
  9. // global variables
  10. int array[41];
  11.  
  12. int main()
  13. {
  14.  
  15. int i = 0;
  16. int max_read = 41;
  17. int amountRead = 0;
  18. int PartNumber, Index;
  19. char Algorithm;
  20.  
  21. // set array elements to -1
  22. for(i = 0; i < 41; i++)
  23. {
  24.  
  25. array[i] = -1;
  26.  
  27. }
  28.  
  29. std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
  30.  
  31. if(!inputfile)
  32. {
  33.  
  34. std::cout<<"Could not open file"<<std::endl;
  35. return 1;
  36.  
  37. }
  38. // prompt for input from user to choose algorithm
  39.  
  40. cout<<"Please select Algorithm\n";
  41. cout<<"1. Modulo Division using linear probing \n";
  42. cout<<"2. Modulo Division using key offset \n";
  43. cout<<"3. PsuedoRandom using linear probing \n";
  44. cout<<"4. Rotation using linear probing \n";
  45. cin >> Algorithm;
  46.  
  47.  
  48.  
  49. //this is where we are reading in the information into our array
  50. for(i = 0; i < 41; i++)
  51. {
  52.  
  53. // as you read each part number in the file
  54. inputfile >> PartNumber;
  55.  
  56. // HashAddress function returns array index and it is assigned to variable Index
  57. Index = HashAddress(PartNumber,Algorithm);
  58.  
  59. // put part number in array
  60. array[Index] = PartNumber;
  61.  
  62. }
  63.  
  64. return 0;
  65.  
  66. }
  67.  
  68. // function HashAddress returns location for hash table
  69. int HashAddress(int PartNumber, char Algorithm)
  70. {
  71.  
  72. int Index, count;
  73. int x, y, z;
  74.  
  75.  
  76. // use algorithms to hash address
  77. switch(Algorithm)
  78. {
  79. case '1': // first algorithm, Modulo Division with linear probing
  80. Index = PartNumber % 41;
  81.  
  82.  
  83. break;
  84.  
  85. case '2': // second algorithm, Modulo Division with key offset
  86. Index = PartNumber % 41;
  87.  
  88. break;
  89.  
  90. case '3': // third algorithm, Psuedorandom with linear probing
  91. Index = (17 * PartNumber + 7) % 41;
  92.  
  93. break;
  94.  
  95. case '4': // fourth algorithm, Rotation with linear probing
  96. x = PartNumber % 10;
  97. y = PartNumber / 10;
  98. z = x * 10;
  99. Index = (z+y) % 41;
  100.  
  101. break;
  102.  
  103. default:
  104. cout << "Invalid Selection\n"; //invalid choice
  105. }
  106.  
  107. cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index;
  108.  
  109. // check to see if array element is occupied
  110. if(array[Index] < 0)
  111.  
  112. // array element is available so return the address/location
  113. return Index;
  114.  
  115. else //collision, part number is already in that spot
  116. {
  117. count = 0; // use count to stop if table is full
  118.  
  119. do
  120. {
  121.  
  122. switch(Algorithm)
  123. {
  124. case '1':
  125. Index = (Index + 1) % 41; // % 41 allows for wrap around
  126. break;
  127.  
  128. case '2':
  129.  
  130. Index = (PartNumber / 41) % 41;
  131.  
  132. case '3':
  133. Index = (Index + 1) % 41; // % 41 allows for wrap around
  134. break;
  135.  
  136. case '4':
  137. Index = (Index + 1) % 41; // % 41 allows for wrap around
  138. break;
  139.  
  140. default:
  141. cout << "Invalid Selection\n"; //invalid choice
  142. }
  143. count ++;
  144. } while ((array[Index] > 0) && (count < 41));
  145.  
  146.  
  147.  
  148.  
  149. if (count == 41)
  150. {
  151. //print ERROR hash table is full
  152. cout << "Hash table is full. Can not insert part number.";
  153. }
  154. else
  155. cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index;
  156.  
  157. return Index;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }


This is because when you are reading the numbers from the file you are trying to read 41 characters..but only 31 characters are present in the file.. so it is reading the last cahracter 10 more times.Try to decrease the value of i in the for loop and you will get your desired result.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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