// 1. Define a 3*3 two-dimensional array
// 2. input the content and output it on the screen
// 3. Use the nested for loop to transpose the array and Output
(1) My program: using intermediate traffic switching-incorrect version: The transpose algorithm has a problem. You need to simulate it.
# Include <iostream> using namespace STD; int main () {int temp; int A [3] [3]; cout <"Please input 9 Numbers:" <Endl; for (INT I = 0; I <3; I ++) // used for input content {for (Int J = 0; j <3; j ++) {CIN> A [I] [J] ;}} for (INT m = 0; m <3; m ++) // output the original matrix {for (INT n = 0; n <3; n ++) {cout <A [m] [N] <"";} cout <Endl ;}cout <Endl; For (INT p = 0; P <3; p ++) // used to transpose an array to {for (INT q = 0; q <3; q ++) {temp = A [p] [Q]; A [p] [Q] = A [Q] [p]; A [Q] [p] = temp ;}}for (INT x = 0; x <3; X ++) // used to output the transposed matrix {for (INT y = 0; y <3; y ++) {cout <A [x] [Y] <"";} cout <Endl;} return 0 ;}
-Correct version: Correct the transpose Algorithm
For (INT p = 0; P <3; p ++) // used to transpose an array {for (INT q = 0; q <p; q ++) // here we have changed {temp = A [p] [Q]; A [p] [Q] = A [Q] [p]; A [Q] [p] = temp ;}}?
(2) using an intermediate array:
# Include <iostream> using namespace STD; int main () {int temp; int A [3] [3]; int B [3] [3]; // set the intermediate array to transpose cout <"Please input 9 Numbers:" <Endl; For (INT I = 0; I <3; I ++) // input content {for (Int J = 0; j <3; j ++) {CIN> A [I] [J] ;}} for (INT m = 0; m <3; m ++) // used to output the original matrix {for (INT n = 0; n <3; n ++) {cout <A [m] [N] <";}cout <Endl; For (INT p = 0; P <3; P ++) // used to transpose an array {for (INT q = 0; q <3; q ++) {B [Q] [p] = A [p] [Q] ;}} for (INT x = 0; x <3; X ++) // used to output the transposed matrix {for (INT y = 0; y <3; y ++) {cout <B [x] [Y] <"";} cout <Endl;} return 0 ;}
(3) Indirect transpose: After storing the data in the array, the indirect output transpose the array.
# Include <iostream> using namespace STD; int main () {int temp; int A [3] [3]; cout <"Please input 9 Numbers:" <Endl; for (INT I = 0; I <3; I ++) // used for input content {for (Int J = 0; j <3; j ++) {CIN> A [I] [J] ;}} for (INT m = 0; m <3; m ++) // output the original matrix {for (INT n = 0; n <3; n ++) {cout <A [m] [N] <"";} cout <Endl ;}cout <Endl; For (INT x = 0; x <3; X ++) // returns the transpose matrix {for (INT y = 0; y <3; y ++) {cout <A [y] [x] <""; // indirect output transpose matrix} cout <Endl;} return 0 ;}