It's because the row and column have different dimensions, so when you swap matrix[r][c] with matrix[c][r], you're trying to put 10 elements where you only have room for 5.
edit:
I suppose I should tell you how to fix it too.
I would use a 1-dimensional array with ROW*COL elements. Then you can subscript it with [r*COL+c]