| | |
count number of comparisons
![]() |
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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:
Thank you in advance!
PS: sorry for my poor english!
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define MAXSIZE 20 #define MAXLEN 20 int seq_Search(char array[][MAXSIZE], int count, char *key); int main(void) { char target[MAXLEN]; char words[MAXLEN][MAXSIZE]; FILE *file; if((file = fopen("a3.txt", "r")) == NULL){ printf("Cannot open file\n"); } else{ int i = 0; while(!feof(file)){ fscanf(file, "%s", words[i]); } i++; } printf("\n\nInput: "); scanf("%s", target); int location = seq_Search(words, 15, target); if((strcasecmp(words[location], target)) == 0){ printf("\nWord %s found", target); printf("Location: %d\n", location); } else{ printf("\nWord %s cannot be found ", target); printf("Location: %d\n", location); } } return 0; } int seq_Search(char array[][MAXSIZE], int end, char *key) { int mid; int first = 0; int last = end; while(first <= last){ mid = (first + last) / 2; if(strcasecmp(array[mid], key) < 0) { first = mid + 1; } else if(strcasecmp(array[mid], key) > 0) { last = mid - 1; } else break; } return mid; }
Thank you in advance!
PS: sorry for my poor english!
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
0
#2 29 Days Ago
Save the result of the string compare so you wont do it twice. Then just increment a counter.
C Syntax (Toggle Plain Text)
int seq_Search(char array[][MAXSIZE], int end, char *key) { int mid; int first = 0; int last = end; int cmp; while(first <= last){ mid = (first + last) / 2; cmp = strcasecmp(array[mid], key); g_Count++; if(cmp < 0) { first = mid + 1; } else if(cmp) > 0) { last = mid - 1; } else break; } return mid; }
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
0
#3 29 Days Ago
•
•
•
•
Save the result of the string compare so you wont do it twice. Then just increment a counter.
C Syntax (Toggle Plain Text)
int seq_Search(char array[][MAXSIZE], int end, char *key) { int mid; int first = 0; int last = end; int cmp; while(first <= last){ mid = (first + last) / 2; cmp = strcasecmp(array[mid], key); g_Count++; if(cmp < 0) { first = mid + 1; } else if(cmp) > 0) { last = mid - 1; } else break; } return mid; }
Thank you very much for your reply, but it seems the code that you showed me didn't work for me!
(((( ![]() |
Other Threads in the C Forum
- Previous Thread: How to get the "Program Counter"
- Next Thread: kernel modules
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





