You declare val2[] as a 100-element array, but you use it as 256-element array. val1, ind1 and ind2 are also at risk of being addressed out of range.
I understand you get an output - but is this output even correct? You're lucky it doesn't crash. Reading/writing variables outside of their assigned memory is a recipe for disaster.
Without clear variable names or comments on what is what, I can't figure out what it is supposed to do, nor what it is actually doing.