You're algorithm is putting all the values of ArrayB into every spot in MergedArray... Use a counter that is updated in the main loop.


// merging the second half of the array
int counter =...