944,097 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 647
  • C RSS
Oct 31st, 2009
0

count number of comparisons

Expand Post »
Hello guys!

I'm new to C language and I need help to write a program which searches specific word in text file using Binary Search and count the number of comparisons!!!

My text file consist of following data:
apple
book
clock
dog
elephant
fat
hello
key
lucky
moon
olive
paper

So far, i made program which searches word and gives the location (index) of this word in file. However, that's not what I need, I need to count the number of comparisons!

I'm currently here:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAXSIZE 20
  5. #define MAXLEN 20
  6.  
  7.  
  8. int seq_Search(char array[][MAXSIZE], int count, char *key);
  9.  
  10. int main(void)
  11. {
  12. char target[MAXLEN];
  13. char words[MAXLEN][MAXSIZE];
  14. FILE *file;
  15.  
  16.  
  17. if((file = fopen("a3.txt", "r")) == NULL){
  18. printf("Cannot open file\n");
  19. }
  20.  
  21. else{
  22.  
  23. int i = 0;
  24.  
  25. while(!feof(file)){
  26. fscanf(file, "%s", words[i]);
  27. }
  28. i++;
  29. }
  30.  
  31.  
  32. printf("\n\nInput: ");
  33. scanf("%s", target);
  34.  
  35.  
  36.  
  37. int location = seq_Search(words, 15, target);
  38.  
  39. if((strcasecmp(words[location], target)) == 0){
  40. printf("\nWord %s found", target);
  41. printf("Location: %d\n", location);
  42. }
  43. else{
  44. printf("\nWord %s cannot be found ", target);
  45. printf("Location: %d\n", location);
  46. }
  47.  
  48.  
  49. }
  50. return 0;
  51. }
  52.  
  53. int seq_Search(char array[][MAXSIZE], int end, char *key)
  54.  
  55. {
  56. int mid;
  57. int first = 0;
  58. int last = end;
  59.  
  60. while(first <= last){
  61. mid = (first + last) / 2;
  62.  
  63. if(strcasecmp(array[mid], key) < 0)
  64. {
  65. first = mid + 1;
  66. }
  67.  
  68. else if(strcasecmp(array[mid], key) > 0)
  69. {
  70. last = mid - 1;
  71. }
  72.  
  73. else
  74. break;
  75.  
  76. }
  77. return mid;
  78. }

Thank you in advance!

PS: sorry for my poor english!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
timaquerra is offline Offline
10 posts
since Oct 2009
Oct 31st, 2009
0
Re: count number of comparisons
Save the result of the string compare so you wont do it twice. Then just increment a counter.

  1.  
  2. int seq_Search(char array[][MAXSIZE], int end, char *key)
  3.  
  4. {
  5. int mid;
  6. int first = 0;
  7. int last = end;
  8. int cmp;
  9.  
  10. while(first <= last){
  11. mid = (first + last) / 2;
  12. cmp = strcasecmp(array[mid], key);
  13. g_Count++;
  14.  
  15. if(cmp < 0)
  16. {
  17. first = mid + 1;
  18. }
  19.  
  20. else if(cmp) > 0)
  21. {
  22. last = mid - 1;
  23. }
  24.  
  25. else
  26. break;
  27.  
  28. }
  29. return mid;
  30. }
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008
Nov 1st, 2009
0
Re: count number of comparisons
Click to Expand / Collapse  Quote originally posted by SVR ...
Save the result of the string compare so you wont do it twice. Then just increment a counter.

  1.  
  2. int seq_Search(char array[][MAXSIZE], int end, char *key)
  3.  
  4. {
  5. int mid;
  6. int first = 0;
  7. int last = end;
  8. int cmp;
  9.  
  10. while(first <= last){
  11. mid = (first + last) / 2;
  12. cmp = strcasecmp(array[mid], key);
  13. g_Count++;
  14.  
  15. if(cmp < 0)
  16. {
  17. first = mid + 1;
  18. }
  19.  
  20. else if(cmp) > 0)
  21. {
  22. last = mid - 1;
  23. }
  24.  
  25. else
  26. break;
  27.  
  28. }
  29. return mid;
  30. }


Thank you very much for your reply, but it seems the code that you showed me didn't work for me! ((((
Reputation Points: 10
Solved Threads: 0
Newbie Poster
timaquerra is offline Offline
10 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to get the "Program Counter"
Next Thread in C Forum Timeline: kernel modules





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC