Ši programa iš vartotojo paima r * c eilės matricą ir apskaičiuoja matricos perkėlimą.
Norėdami suprasti šį pavyzdį, turėtumėte žinoti šias C ++ programavimo temas:
- C ++ masyvai
- C ++ daugiamatės masyvai
Šioje programoje vartotojo prašoma įvesti eilučių ir stulpelių skaičių. Eilučių ir stulpelių vertė šioje programoje turėtų būti mažesnė nei 10.
Tada vartotojo prašoma įvesti matricos elementus.
Programa apskaičiuoja matricos perkėlimą ir parodo ją ekrane.
Pavyzdys: raskite „Matrix“ perkėlimą
#include using namespace std; int main() ( int a(10)(10), transpose(10)(10), row, column, i, j; cout <> row>> column; cout << "Enter elements of matrix: " << endl; // Storing matrix elements for (int i = 0; i < row; ++i) ( for (int j = 0; j < column; ++j) ( cout << "Enter element a" << i + 1 << j + 1 <> a(i)(j); ) ) // Printing the a matrix cout << "Entered Matrix: " << endl; for (int i = 0; i < row; ++i) ( for (int j = 0; j < column; ++j) ( cout << " " << a(i)(j); if (j == column - 1) cout << endl << endl; ) ) // Computing transpose of the matrix for (int i = 0; i < row; ++i) for (int j = 0; j < column; ++j) ( transpose(j)(i) = a(i)(j); ) // Printing the transpose cout << "Transpose of Matrix: " << endl; for (int i = 0; i < column; ++i) for (int j = 0; j < row; ++j) ( cout << " " << transpose(i)(j); if (j == row - 1) cout << endl << endl; ) return 0; )
Rezultatas
Įveskite matricos eilutes ir stulpelius: 2 3 Įveskite matricos elementus: įveskite elementą a11: 1 įveskite elementą a12: 2 įveskite elementą a13: 9 įveskite elementą a21: 0 įveskite elementą a22: 4 įveskite elementą a23: 7 įveskite matricą: 1 2 9 0 4 7 Matricos perkėlimas: 1 0 2 4 9 7