r/cprogramming • • Apr 24 '21

Need help with this code, Its giving garbage value

0 Upvotes

8 comments sorted by

13

u/atsju Apr 24 '21

Seriously, don't share print screens. And please what is the problem ? Expectations VS results

1

u/[deleted] Apr 24 '21

While sorting the elements the first element is garbage value and theast element of array is not showing up

3

u/atsju Apr 24 '21 edited Apr 24 '21

This sounds like overflow. You access outside the array boundaries. That's all. Do some prints or use debugger to understand what cell of the array your code accesses and you will know where is shouldn't access.

General advise : use define for 10, pass the length to your function or it won't work for different length, check boundaries in all loops, prefer for loops where it's clearer (2nd and 3rd while).

Edit: and comment your code. For other people and also for yourself.

Edit2: I digged in an have a better general view now, but didn't spot the problem exactly. You use a recursive function calling merge each time. Merge creates an array size 10 even when it doesn't need 10 each time. Moreover, it should be possible to displace elements in place instead using a full buffer array. Here you will need something like 100bytes of B arrays in the end

1

u/zCybeRz Apr 24 '21

If the array is size 10, the maximum index is 9. You could change the value passed in to h-1, but it's more common to pass in the size and use < instead of <= when iterating it

0

u/[deleted] Apr 24 '21

Changing the passed value to h-1 solved it.Thanks

2

u/atsju Apr 24 '21

I found the (or at least one big enough to cause issues) error. As I said in first post, you access array outside it's boundaries. Check them better. I won't tell you exactly where because it wouldn't help you in long term.

So find out how to use a debugger because you will need it in long term and it will really help you more than the answer. Programming is more like 90% debugging and testing.

Edit : I really don't want to be mean by not giving the answer right away. Do not hesitate to ask if you are still struggling in some hours.

1

u/ptchinster Apr 24 '21

!picoftext

1

u/rtlcprogbot Apr 24 '21

!picoftext

For the love of fuck dont give us a picture of text.

I am a bot. Replying to me notifies nobody