![]() |
|
#1
|
|||
|
|||
Problem with sorting algorithmI had this assingment from my profesor in COS:
The program has to scan an input text, select every word (separated with white space or a punctuation characters); tofind all occurrences of a particular word(address of the word is the number of its first character); to create the index( word with attached list of addresses); and to sort words (with index alphabetically. So far i have managed to solve every problem exept the sorting of words. Can someone help me fixing the bugs in the sorting algorithm. Here is the code: CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Problem with sorting algorithmWith a quick look, it appears that your while loop is, at least at first, killing you. It is not breaking up the string into words as expected, but keeps appending the entire string to the array.
If you want a possibly easier way to at least break up the words (but they will not be sorted, you will still need to do that) take a look at the strtok() function example in the MSDN. |
|
#3
|
|||
|
|||
Re: Problem with sorting algorithmThanks very much TurboPT. I think that will solve my problem.
|
|
#4
|
|||
|
|||
Re: Problem with sorting algorithmQuote:
I hope that I can say a few things about your code without offending you. There are a number of "style" issues that you should really correct with your programming style, IMO. Somewhere around 2200 years ago, whitespace was added to make sentences clearer and easier to read. It is a good tradition and I recommend it both for writing communications and coding. Programmers who tend to jam everything together in statements are not using whitespace for its intended purpose: Make code clearer and easier to read. CPP / C++ / C Code:
This isn't C++, it is C with some C++ features being used and mixed with C. By choosing to use C++ you can greatly reduce your programming difficulty. CPP / C++ / C Code:
...use a space after every comma. Also, the preferred pointer syntax in C++ is for the asterisk to cuddle the type name: CPP / C++ / C Code:
In C, the convention is to cuddle the variable name. Whichever you choose, do it consistently and with a space after each comma. Okay? Try to declare variables one per line. Multiple variables on the same line are more difficult to maintain over time. CPP / C++ / C Code:
I recommend separating your variable declarations from your for conditional blocks. It makes code easier to work on and debug by allowing you to easily comment out a block by adding /* and */ in the blank line locations. Do you realize that you don't have a single comment in "bmsearch" ...not even one that describes what bmsearch is supposed to do? Your variable names are not very expressive. Granted, most of us can figure out what "len" is, but "ptct" and "compcount" ...what are they? If you are asking for help from other programmers, then you help them first by making your code readable from the outset. It won't kill you to spell out whatever ptct is and why it should be initialized to 1 less than length. Another really good habbit to get into is to always use braces around conditional blocks. Here is an example: CPP / C++ / C Code:
...not only does it make the code more readable, it is much easier to add something inside of it to check the state of a variable. Also, if something needs to be added to the code, multi-line blocks are already "ready" for it. CPP / C++ / C Code:
...lines like this are difficult to read and understand from a glance. CPP / C++ / C Code:
...is a little better, isn't it? Ideally, something that reduces the complexity of code should be considered. There are a lot of less than ideal choices made in your C-code. For example, you're using cout to output and gets to input? Why not simply use cin? Another example of not-very-good-design is that you're doing a lot of your "algorithm" work inside of your main (which should be declared to return an int). I don't know who your professor is, but I'm guessing that after reaching tenure reading this level of code, he probably is immune to it. Here's a good idea: Separate your work into discrete functions and do the work inside of the functions. Make each function that you use perform a simple task so that the complexity of the code is greatly reduced. Use as many functions as you need to break down the job into small "bite-sized" bits of work. Worry less about looping a lot and coding inefficiencies and worry more about making each little function easy to read and understand what happens. C is a low-level language. You have to build little modules of functionality to accomplish larger jobs. Trying to do too much in one function complicates the coding and the debugging. Chain the work together inside of main so that main initializes the variables and then, as needed, passes them in to the functions that perform work on the variables. Create your functions based on the variables that they need in order to do the work. If you can succeed at doing what I just wrote, your programs will greatly improve and others will be much more inclined to help out when you have a problem. Also, you can point them to a nice, composite little function and say "this isn't working" and it will be clearer and easier to understand where you are in the work-chain. Unless you expect the user to input a very large amount of text, or eventually read some file, then processing power is probably not a major issue. Breaking down the work will simplify your life, too. :davis: |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Graphic problem in Unreal Tournament 2004 | zerox | Computer Software Forum - Games | 10 | 09-Oct-2005 13:31 |
| Runtime Problem involving "printf" in C Program | supamakia | C Programming Language | 2 | 09-Oct-2005 11:09 |
| a significant problem after installing Xp | mohammad | Computer Software Forum - Windows | 10 | 09-Aug-2005 08:03 |
| String sorting problem | Lost Boy | C Programming Language | 5 | 10-Apr-2005 19:49 |
| STL (algorithm): for_each problem | yosep | C++ Forum | 1 | 03-Nov-2003 12:36 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The