-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranspose.cpp
More file actions
58 lines (47 loc) · 1.33 KB
/
transpose.cpp
File metadata and controls
58 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
# Descrição:
O objetivo desse algoritmo é construir a matriz tranposta de uma matriz qualquer.
A matriz transposta é contruída por referência na função "transpose".
# Parâmetros:
int A[][MAX] -> Matriz original.
int B[][MAX] -> Matriz transposta.
int M -> Total de linhas da matriz A.
int N -> Total de colunas da matriz A.
# Constantes:
#define MAX 50 -> Dimensão máxima da matriz.
# Retorno
A função não retorna nada mas modifica por referência a matriz B passada como
parâmetro.
# Complexidade:
O(M*N), Onde N é o número de colunas e M é o número de linhas da matriz.
# Referência:
https://www.geeksforgeeks.org/program-to-find-transpose-of-a-matrix/
# Problemas:
Problema B - Simulado UFU - 30/07/2022 (Grid)
*/
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
void transpose(int A[][MAX], int B[][MAX], int M, int N)
{
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
B[i][j] = A[j][i];
}
int main()
{
int M = 3, N = 4;
int A[MAX][MAX] = {{ 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 }};
int B[MAX][MAX];
transpose(A, B, M, N);
cout << "Matriz transposta: " << endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout << " " << B[i][j];
cout << "\n";
}
return 0;
}