Reputation: 77
array:
1 2 3
4 5 6
The output I want:
1 4
2 5
3 6
My code:
for (i = 0; i < rows; i++) {
for (j = 0; j < col; j++) {
temp = matrix[j][i];
matrix[j][i] = matrix[i][j];
matrix[i][j] = temp;
}
}
for (i = 0; i < col; i++) {
for (j = 0; j < rows; j++) {
cout << setw(5) << matrix[i][j];
}
cout << endl;
}
where number of rows and columns, and the elements of array were taken from the user.
The output I am getting:
1 2
4 5
3 6
Any and every help will be appreciated.
Upvotes: 0
Views: 486
Reputation: 1197
Try making for (j = i; j < col; j++)
instead of for (j = 0; j < col; j++)
in your matrix loop, because you should not go back to values that were already changed.
Upvotes: 1