plzzzzzz... i need a help in natural merge sort

This is a discussion on plzzzzzz... i need a help in natural merge sort within the C++ Programming forums, part of the General Programming Boards category; hello for everyone here
well i am a fresh man here ... and i'm really pleased that i'm a member ...

plzzzzzz... i need a help in natural merge sort

hello for everyone here
well i am a fresh man here ... and i'm really pleased that i'm a member here
well .. i made a program in c++ which is all about natural merge sort on files , since i want to sort my file which contains a randolmy generated words (100 , 1000 .... words , according to Nmax value)
so .. i store my data in the file then call the merge sort function
but the problem is that when my data get bigger(i.e. more than 100 words) my program does a partial sorting on'em , and i think i gets into an infinet loop cuz it does not end successfully ....... so i'm really astonished why!! cuz i'm almost sure that my program is bug-free
so please guyz help me ... and thank you in advance

Forget trying to be sure your program is bug-free. Without rigorous unit tests your code wont be bug free. And even then...

Those '/0' multicharacter literals are probably supposed to be '\0' for starters, and I noticed that in 5 seconds.
I don't have time to go over this thoroughly, but I'll so that in an hour when I'm back again.

The last 'if' and the following 'else if' are tesing the exact same thing, therefore the second one that returns true can never be hit

Not all code paths return a value

Lack of const-correctness on the input parameters

I would have let you off on the last one if this were the C forum though.
Not to mention, the first two functions are attempting to do the job of functions that are part of std::string, which you should be using. But I'll refrain from going as far as to call that one a bug as well.

A furthur quick glance shows that you're not checking if any of your files were opened successfully either. There's another 8 bugs for you.

You can drop the second "using namespace std;".

Could you fix the indentation for the rest of your program please. Replace tabs with a fixed number of spaces before posting.

Hi iMalc thanx for your notes about my program, and sorry cuz I said it is a bug-free
I did as you told me and corrected my bugs in the program but that didn't work, it still sort the elements partially and not printing my elements when it finishes… you told me also to use the std::string, I considered that but I discovered that I cannot cuz initially I am randomizing the characters and ,assigning 'em to the array of char, and storing it in the file, so when I wanna read from the file using a String variable , it says that conversion cannot be made between char[] and string !!!
Thank you very much again ..

merge and distribute are a little hard to follow though because they're a bit long. Perhaps you could look for some lines of code that are used a few times and put them in their own function to simplify things. For example, something like at least these lines of code:

You can put that in a function and pass wrd, w1, and f1 as parameters (probably passed by reference). Then if you name that function appropriately and use it in as many places as it makes sense to, your code should look simple enough for anyone to follow and hopefully spot whatever bug you have.

Good, the code looks a little easier to follow like that, but it did unfortunately introduce one litle bug:

Code:

void copy_sequence(fstream& f1,fstream& f2, char w1[6], string w2)

w2 needs to be pass-by-reference here. Otherwise w2 = w1; wont do anything to the variable passed in.
Could you give a better description that "Not working" please. That could mean a million different things. Does the program start? Does it get stuck in an infinite loop? Does it output garbage? Does it output the items in a slightly wrong order? Does it output the items in backwards order? Does it take too long? Does it cause an access violation and terminate?

I don't have time to run the whole program at the moment, but maybe after work. Many of us rely on a good description of what happens to resolve the bug rather than actually running it.

hii bro iMalc.. here is a description of what happens to my program:
when it finishes randomizing words (i print the randomized words so i can see 'em before storing in the file) it should sort all the words strored in my file and then prints them all sorted ascending.. but the problem is that my program stops at a step i don't know what is it(but i think in the sorting step) , and this "not knowing" is because debugging a program, which sorts 100 words or more to know where the bug is, is impossible . and as a result to this bug, it does not print any word in the file..
well after i terminate my program by closing it .. i go to the created binary file in oreder to see what happened (i open it with notepad), i see that it has been sorted partiallly (i.e. in case of 1000, every chunk of 100 or 150 words has been sorted alone)
thank you agian bro for taking your time

Okay I've had a good look through the code and I can see one likely problem.
My guess is that the do while (!end_sequence) loop is going into an infinite loop. This would happen if the program never goes into the if statements that set end_sequence to true, which happens if wrd is not greater than w1 or w2 and neither file is at eof.
You need more practice at explaining what happens though. Programs don't really just 'stop'. They tend to either crash or go into an infinite loop. Maybe you just didn't know what to call it. Either should be easy to debug especially if you are using a small enough amount of data.

For example, try decreasing Nmax to the smallest possible value that causes the program to not work. Start even as low as 1, and if that doesn't work proplerly, then it will be really easy to step through the code one line at a time until you see what it does wrong.
If it works with 1, then try 2, then 3 etc. Run each a couple of times to be sure.
Lets say you get up to 5 and then it doesn't sort properly sometimes. note down the starting order of the words that failed and then temporarily comment out the code that generates random words and instead put in code to hard-code just those 5 words. Then you will have something that fails every time. Now step through the code one line at a time. Learn the difference between step-over and step-into. You may wish to step over calls to compare for example.

Note that it can even be a good idea to make sure that it works with zero items too! Quite often in the real world the container of data to sort is empty, and using a do while rather than a while in certain places can lead to code that wont work in such a case.

If you're under a lot of time pressure then I'll fire it up in Visual Studio next. For the moment though, I'm hoping I can help enhance your skills such that you are capable of solving more issues on your own, which is all good.

hi iMalc , well i did my best trying to know what the hell is wrong with my program, but i couldn't figure out the bug that , most likely, gets my program into an infinite loop.
i tried Nmax up to 90 elements, in most cases it did not cause any problem, but when Nmax gets bigger , my program does not give any results ..
i figured out a thing why my program is "chunkly" sorted.. cuz when it gets into an infinite loop, it would have done some sorting stages(merge and distribute) so in a step i dunno what it is it goes infinitely.
one more thing .. when i terminate the program and go to where my 3 files located i see a thing... sometimes i see that file1 is empy and the other two files are not(it could be an indicator that the infinity occures in the merging phase), and sometimes i see that file2 and file3 are empty while file1 is not!(it could be an indicator that the infinity occures in the distributing phase)..
this is all i can say about my program .. soo thanks again for your effort bro

I ran your program to see the contents of the file and discovered that using !myfile.eof() will read the last record twice. So, if you are using this condition during the merge, it will most likely cause problems.