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 ();
}