>Sorry. I didn't read your code carfully enough before I replied.
Maybe you should run example code to see if it works before trying to correct it with an incorrect solution. Starting the inner loop at 0 instead of i + 1 will result in a lot of shuffling to get the same matrix that you started with, not a transposed matrix as the OP requested. So maybe you should also test your own code before posting it. Especially if you're trying to correct someone and using it as an example.
On a purely stylistic point, I have two issues with your code. First, even though it's valid the way you did it, you should always put braces around a loop or if construct that has more than a one line body. This way you don't have to rely solely on indention to prove that your code is correct. You also avoid certain pitfalls.
Second, starting the statements of a block on the same line as the opening brace is a formatting nightmare. It makes code harder to read and harder to reformat so that it's easier to follow.
Something more like this (following your apparent brace indention style):
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
save = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = save;
You also failed to define save before using it, but I'll let you slide on that. ;)
I read you method of transposing a matrix
but i have toa sk a question!
that what is the logic behind initializing inner loop variable j=i+1 instead of j=0???
i shalll be thankful to you if you reply to this post
You only need to loop through less-than-half of the matrix since in each execution of the inner loop you're swapping two elements (and the main diagonal stays put). If you iterated through the whole matrix, you'd just put it back the way it started. Starting j at i+1 loops through the upper triangle of the square (or lower, depending on how you look at it), swapping elements with the opposite triangle.