#include <stdio.h>
#include <stdlib.h>
int alocaMatriz(int ***matriz, int lin, int col) //agora com ponteiro para int**
{
int i;
*matriz
=malloc(lin
*sizeof(int*)); //com * alterar o valor apontado
for(i=0; i<col; i++)
{
(*matriz
)[i
]=(int*)malloc(col
*sizeof(int)); //aqui também }
return 0;
}
void leMatriz(int **matriz, int lin, int col)
{
int i, j;
printf("\nDigite os valores: ");
for(i=0; i<lin; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &matriz
[i
][j
]); }
}
}
void mostraMatriz(int **matriz, int lin, int col)
{
int i, j;
for(i=0; i<lin; i++)
{
for(j=0; j<col; j++)
{
}
}
}
int ordenaMatriz(int **matriz, int lin, int col)
{
int i, j, menor, cont=0, indice;
int vetor[lin*col];
for(i=0;i<lin;i++){
for(j=0;j<col;j++){
vetor[cont]=matriz[i][j];
cont++;
}
}
for(i=0;i<(lin*col);i++){
}
for(i=0;i<lin*col;i++){
menor=vetor[i];
for(j=0;j<lin*col;j++){
if(vetor[j]<menor){
menor=vetor[j];
int aux = vetor[i];
vetor[i] = menor;
vetor[j] = aux;
}
}
}
for(i=0, cont = 0;i<lin;i++){
for(j=0;j<col;j++){
matriz[i][j]=vetor[cont];
cont++;
}
}
}
int main(int argc, char** argv)
{
int lin, col, i;
int **matriz;
printf("Digite as dimensoes da matriz: "); scanf("%d %d", &lin
, &col
);
alocaMatriz(&matriz, lin, col);
leMatriz(matriz, lin, col);
mostraMatriz(matriz, lin, col);
ordenaMatriz(matriz, lin, col);
printf("\n\nMatriz ordenada:\n\n"); mostraMatriz(matriz, lin, col);
//desalocaMatriz(&*matriz, lin, col);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBhbG9jYU1hdHJpeihpbnQgKioqbWF0cml6LCBpbnQgbGluLCBpbnQgY29sKSAgLy9hZ29yYSBjb20gcG9udGVpcm8gcGFyYSBpbnQqKgp7CiAgICBpbnQgaTsKCiAgICAqbWF0cml6PW1hbGxvYyhsaW4qc2l6ZW9mKGludCopKTsgLy9jb20gKiBhbHRlcmFyIG8gdmFsb3IgYXBvbnRhZG8KCiAgICBmb3IoaT0wOyBpPGNvbDsgaSsrKQogICAgewogICAgICAgICgqbWF0cml6KVtpXT0oaW50KiltYWxsb2MoY29sKnNpemVvZihpbnQpKTsgLy9hcXVpIHRhbWLDqW0KICAgIH0KICAgIHJldHVybiAwOwp9Cgp2b2lkIGxlTWF0cml6KGludCAqKm1hdHJpeiwgaW50IGxpbiwgaW50IGNvbCkKewogICAgaW50IGksIGo7CgogICAgcHJpbnRmKCJcbkRpZ2l0ZSBvcyB2YWxvcmVzOiAiKTsKCiAgICBmb3IoaT0wOyBpPGxpbjsgaSsrKQogICAgewogICAgICAgIGZvcihqPTA7IGo8Y29sOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBzY2FuZigiJWQiLCAmbWF0cml6W2ldW2pdKTsKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgbW9zdHJhTWF0cml6KGludCAqKm1hdHJpeiwgaW50IGxpbiwgaW50IGNvbCkKewogICAgaW50IGksIGo7CgogICAgZm9yKGk9MDsgaTxsaW47IGkrKykKICAgIHsKICAgICAgICBmb3Ioaj0wOyBqPGNvbDsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCIlZCIsIG1hdHJpeltpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKaW50IG9yZGVuYU1hdHJpeihpbnQgKiptYXRyaXosIGludCBsaW4sIGludCBjb2wpCnsKCiAgICBpbnQgaSwgaiwgbWVub3IsIGNvbnQ9MCwgaW5kaWNlOwoJaW50IHZldG9yW2xpbipjb2xdOyAKCQoJZm9yKGk9MDtpPGxpbjtpKyspewoJICAgIGZvcihqPTA7ajxjb2w7aisrKXsKCSAgICAgICAgdmV0b3JbY29udF09bWF0cml6W2ldW2pdOwoJICAgICAgICBjb250Kys7CgkgICAgfQoJfQoJCglmb3IoaT0wO2k8KGxpbipjb2wpO2krKyl7CgkgICAgcHJpbnRmKCIlZCAiLCB2ZXRvcltpXSk7Cgl9Cglmb3IoaT0wO2k8bGluKmNvbDtpKyspewoJICAgIG1lbm9yPXZldG9yW2ldOwoJICAgIGZvcihqPTA7ajxsaW4qY29sO2orKyl7CgkgICAgICAgIGlmKHZldG9yW2pdPG1lbm9yKXsKCSAgICAgICAgICAgIG1lbm9yPXZldG9yW2pdOwoJICAgICAgICAgICAgaW50IGF1eCA9IHZldG9yW2ldOwoJICAgICAgICAgICAgdmV0b3JbaV0gPSBtZW5vcjsKCSAgICAgICAgICAgIHZldG9yW2pdID0gYXV4OwoJICAgICAgICB9CgkgICAgfQoJfQoJZm9yKGk9MCwgY29udCA9IDA7aTxsaW47aSsrKXsKCSAgICBmb3Ioaj0wO2o8Y29sO2orKyl7CgkgICAgICAgIG1hdHJpeltpXVtqXT12ZXRvcltjb250XTsKCSAgICAgICAgY29udCsrOwoJICAgIH0KCX0KCn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyKiogYXJndikKewogICAgaW50IGxpbiwgY29sLCBpOwogICAgaW50ICoqbWF0cml6OwoKICAgIHByaW50ZigiRGlnaXRlIGFzIGRpbWVuc29lcyBkYSBtYXRyaXo6ICIpOwogICAgc2NhbmYoIiVkICVkIiwgJmxpbiwgJmNvbCk7CgogICAgYWxvY2FNYXRyaXooJm1hdHJpeiwgbGluLCBjb2wpOwogICAgbGVNYXRyaXoobWF0cml6LCBsaW4sIGNvbCk7CiAgICBtb3N0cmFNYXRyaXoobWF0cml6LCBsaW4sIGNvbCk7CiAgICBvcmRlbmFNYXRyaXoobWF0cml6LCBsaW4sIGNvbCk7CiAgICBwcmludGYoIlxuXG5NYXRyaXogb3JkZW5hZGE6XG5cbiIpOwogICAgbW9zdHJhTWF0cml6KG1hdHJpeiwgbGluLCBjb2wpOwogICAgLy9kZXNhbG9jYU1hdHJpeigmKm1hdHJpeiwgbGluLCBjb2wpOwoKICAgIHJldHVybiAwOwp9Cg==