Status
Not open for further replies.

NagpurDaMunda

In the zone
In C we normally apply binary search on array ... Is it possible to apply binary search on a text file containing sequence of words in ascending order in C language >?? if yes then how?? pls specify...
 

=CrAzYG33K=

Journeyman
NagpurDaMunda said:
In C we normally apply binary search on array ... Is it possible to apply binary search on a text file containing sequence of words in ascending order in C language >?? if yes then how?? pls specify...

Don't think it's possible ..
But why do you need it in the first place? :D
 

Sykora

I see right through you.
It is possible, but a bit difficult, I think. One obvious method is to load the contents of the entire file into memory, with an array of strings, and use strcmp(). The other way is to search the file in-place, but that's more difficult.
 

Thegame

Broken In
It's possible in many ways!
The simple one is convert all characters to int. (save this int. into array).
And apply ur same basic logic for int. ok!!!!!
It's simple !!!!!
Don't ask how 2 conv. char into int. and how to display.......it's ur job...
 

cooldip10

In the zone
You'll need to use dynamic allocation of variables first of all if you are talikng of any txt file..

BTW r u in school or a pass out!!
 

Zeeshan Quireshi

C# Be Sharp !
NagpurDaMunda said:
i m in school...in 3rd..

well mate no probs u using C++ , but take my advice , to program in C++ u need a lotta time n analytical thinking .

n programming in C++ is very frustrating at first coz there r no High level functions to handle simple tasks , u'll only be wasting ur time n get frustrated urself .

forget C++ for now n cut ur teeth on a better , Easier language like
C# or even PHP , u can return to C++ once u hv a greater understanding of the inner workings of a computer n r ready to program at the system level .

here , try Microsoft's Beginner Developer Learning Center to start out with C#

*msdn.microsoft.com/vstudio/express/beginner/

Well as for the binary search thing , here's what u need to do
Code:
1. I suppose the words in the file r separated by spaces or commas or ny other delimiter
IF words r separated by a space then ur work will be a lot easier
2.Create an object of the ifstream class(fin for me)
3. now use this object to read all the words into a Double-Dimension array ,hence in this DD array each element is a word
4. Now use strcmp() to compare words n do the normal binary search mechanism

i'll be writing the same code in C++ , C# n PHP for u so then u'll understand what i mean by "High Level" functions that make ur work easier , i'll post the code within a day.

File words.txt containing the words said:
abc def ghi jkl mn op qr st unwz yz
PHP Code said:
<?php
$words = file_get_contents("file_get_contents ");
$words = explode(" " , $words);
//Now the variable $Words is a Double-Dimension array with each element as a word
/* now you can do your binary search on this array */

?>
C++ Code said:
#include<fstream>

int main()
{
/*Supposing the file has 10 words , if we didn't know the exact number of words then the program would be MUCH lengthier
also we are supposing that each word is not larger than 10 characters else then too we would hv to add a lot of code to
determine the length of the largest word , also a loop would be needed to determine the number of words then we would
dynamically allocate the size of the array using new() function*/


char words[10][10];
ifstream fin("words.txt");
for ( int i = 0 ; i < 10 ; i++)
fin >> words
// now each element of the array words is a word , hence we can perform our binary search on this
// array using strcmp() function

return 0;
}
 
Last edited:
Status
Not open for further replies.
Top Bottom