• Post Reply Bookmark Topic Watch Topic
  • New Topic

Merging two sorted arrays into a single sorted array  RSS feed

 
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

Enter list1:
5 1 5 16 61 111
Enter list2:
4 2 4 5 6



I tried to implement it also differently but with another error :

 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why are your input lists not sorted?
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:So why are your input lists not sorted?


They are , first inputted integer is the size of the array (you can check the code).
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.

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?
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I see.

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)
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact your last post was so good - you used an excellent debugging technique, you explained the problems you found and how to fix them and then posted the complete solution - I'm awarding you a cow.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!