Advertisement
Riposati

Código

Jul 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /// Autor Gustavo Riposati
  6.  
  7. int main()
  8. {
  9.     int m[5][5];
  10.     int i,j;
  11.     int vet[5];
  12.     srand((unsigned)time(NULL));
  13.  
  14.     /// inicializando a matriz c qlqr numero entre 0 e 100
  15.     for(i=0;i<5;i++){
  16.  
  17.         for(j=0;j<5;j++){
  18.  
  19.             m[i][j] = rand() % 100;
  20.         }
  21.     }
  22.  
  23.     /*********************************/
  24.  
  25.     ///matriz original
  26.     printf("matriz original = \n");
  27.     for(i=0;i<5;i++){
  28.  
  29.         for(j=0;j<5;j++){
  30.  
  31.             printf("%d ",m[i][j]);
  32.         }
  33.  
  34.         printf("\n");
  35.     }
  36.     printf("\n");
  37.  
  38.     /***********************************************/
  39.     for(i=0;i<5;i++){
  40.  
  41.         for(j=0;j<5;j++){
  42.            vet[j] = m[j][i];
  43.            //printf("pos atual = %d\n",m[j][i]);
  44.         }
  45.         ///precisa ordenar aqui depois de pegar a coluna
  46.  
  47.             ///ordenação por bolha, bubble sort
  48.             int aux,z,x;
  49.  
  50.             for(z=0;z<5;z++){
  51.  
  52.                 for(x=0;x<4;x++){
  53.  
  54.                     if(vet[x] > vet[x+1]){
  55.  
  56.                         aux = vet[x+1];
  57.                         vet[x+1] = vet[x];
  58.                         vet[x] = aux;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.         /// mostrando o vetor já ordenado e voltando os valores pra matriz
  64.         int t;
  65.  
  66.         printf("o vetor com os dados da coluna %d ja ordenados\n",i+1);
  67.         for(t=0;t<5;t++){
  68.  
  69.             printf("[%d] ",vet[t]);
  70.             m[t][i] = vet[t];
  71.         }
  72.         printf("\n");
  73.     }
  74.  
  75.     /************************************/
  76.     printf("\n\nmatriz com as colunas ordenadas\n");
  77.     for(i=0;i<5;i++){
  78.  
  79.         for(j=0;j<5;j++){
  80.  
  81.             printf("%d ",m[i][j]);
  82.         }
  83.  
  84.         printf("\n");
  85.     }
  86.  
  87.     system("pause");
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement