The code is meant to input 2 arrays (they must be sorted even if this is not verified ) and then merge them in such a way that a sorted merged array is created at the end.I need to avoid a simple concatenation and then sorting the resulting array operation.I m interested in what i m doing wrong . Considering i lost more then 2 hours on making this work i clearly need help...
The input i used was :
5 1 5 16 61 111
4 2 4 5 6
I tried to implement it also differently but with another error :
Your second attempt is much better than the first. I'm wondering why you're working from back to front though.
Regardless, have you already tried stepping through the program using a debugger?
I started recently programming and i dont have allot of xp using IDEs but i will try that too . You say my second attempt is better , it's closer to a solution yes but i find the first much more clear/clean (that is except if i got it all wrong)
Have you written out in English (or your native language) every step you would take if you explaining to a 5 year old how to do this with a pen and paper.
Once you have written out every step you can convert the steps to code.
I compiled it with pen and paper as i m not very familiar with IDEs .I found the 2 problems in the first example:
- when i for example got to 16 it would still start with j =0 and add again the 2 4 values already added resulting in index overflow error thingy
i fixed it by using a value k in the j for loop like this : int j = k ; and then incrementing k with one in the :
- second issue was that if the the j array (aka list2) was shorter then i array(list1) and his maximum was smaller then the i one the second for loop would end before all i values are processed resulting in last values in the merge array being 0. I fixed this by adding another if in the initial for(i) loop after the nested for (j) ends :
It finally seems to work(bless the gods) , this is the code :
Tiberius Marius wrote:I compiled it with pen and paper as i m not very familiar with IDEs .
Well done, it's really good practice to 'execute' the code on pen and paper it's a great way of finding bugs when you haven't got an IDE and for some problems can even be easier than stepping through the code in an IDE.
And congratulations on finding and solving the problem yourself.
Finally thank you for posting the solution.