Practical training c ++ language design-3*3 matrix transpose Functions

Source: Internet
Author: User

Write and test the 3*3 matrix transpose function, and save the 3*3 matrix using arrays.
Algorithm: first define a two-dimensional array in the main function, and then use the print function to complete the output function. This is done by preference and then transpose, during transpose, you can use an auxiliary array to upload the values in the array to this auxiliary array, and then pass the values in column J of row I to column I of Row J.
Code:
# Include <iostream. h>
Void main ()
{
Int A [3] [3], I, j, B [3] [3];
Cout <"Enter elements in the array in sequence:" <Endl;
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Cin> A [I] [J];
}
}
Cout <"the elements in this matrix are:" <Endl;
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Cout <A [I] [J];
Cout <"";
}
Cout <Endl;
}
Cout <"transposed matrix:" <Endl;
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
B [I] [J] = A [J] [I];
Cout <B [I] [J];
Cout <"";
}
Cout <Endl;
}
}
2. Use dynamic memory allocation to generate dynamic arrays to complete the above functions.
Algorithm: first define an integer pointer in the main function to point it to an array. when outputting elements, you can use the auto-increment pointer to implement transpose, first pass the pointer value, and then complete it in a similar way.
Code:
# Include <iostream. h>
# Include <stdlib. h>
Void print (int *)
{
For (INT I = 0; I <9; I ++)
{
Cout <*;
Cout <"";
A ++;
If (I + 1) % 3 = 0) cout <Endl;
}
}

Void reserve (int * &)
{
Int B [3] [3], I, J;
Int * C;
C =;
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
B [I] [J] = *;
A ++;
}
}
A = C;
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
* A = B [J] [I];
A ++;
}
}
A = C;
}

Void main ()
{
Int I, J;
Int * a, * B;
A = (int *) New int [3] [3];
B =;
Cout <"Enter elements in the array in sequence:" <Endl;
For (I = 0; I <9; I ++)
{
Cin> *;
A ++;
}
A = B;
Cout <"the elements in this matrix are:" <Endl;
Print ();
Reserve ();
Cout <"after transpose, the range is:" <Endl;
Print ();
}

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.