Skip to content

Instantly share code, notes, and snippets.

@danielsaad
Created May 16, 2019 12:07
Show Gist options
  • Save danielsaad/25cf6a8744b882eb98929f2daae9e4fc to your computer and use it in GitHub Desktop.
Save danielsaad/25cf6a8744b882eb98929f2daae9e4fc to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int** aloca_matriz(int l,int c){
int i;
int** matriz;
matriz = malloc(l*sizeof(int*));
for(i=0;i<l;i++){
matriz[i] = malloc(c*sizeof(int));
}
return matriz;
}
void desaloca_matriz(int** matriz,int l){
int i;
for(i=0;i<l;i++){
free(matriz[i]);
}
free(matriz);
}
void preenche_matriz(int** matriz,int l,int c){
int i,j;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
matriz[i][j] = rand()%100;
}
}
}
void calcula_transposta(int** matriz,
int** matriz_transposta,
int l,
int c){
int i,j;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
matriz_transposta[j][i] = matriz[i][j];
}
}
}
void imprime_matriz(int** matriz,int l,int c){
int i,j;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
printf("%3d",matriz[i][j]);
}
printf("\n");
}
printf("\n");
}
int main(void){
int n,m;
printf("Digite as dimensões da matriz: ");
scanf("%d %d",&n,&m);
int** matriz = aloca_matriz(n,m);
int** matriz_transposta = aloca_matriz(m,n);
preenche_matriz(matriz,n,m);
calcula_transposta(matriz,matriz_transposta,n,m);
imprime_matriz(matriz,n,m);
imprime_matriz(matriz_transposta,m,n);
desaloca_matriz(matriz,n);
desaloca_matriz(matriz_transposta,m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment