45. transpose the 3x3 two-dimensional array and Output

Source: Internet
Author: User

// 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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.