Advertisement
xSiRON

ES2_FEB - Stringhe Ordine Non Dec

Feb 2nd, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. /*************************************
  2.    Nome: Alessio Sferro
  3.    File: main.cpp
  4.    Data: 2016-02-02-20.25
  5. *************************************/
  6.  
  7. /*
  8.     Esercizio 2:
  9.         Scrivere un metodo che prenda in input una matrice A di nxm stringhe ed ordini in senso
  10.         non decrescente ogni singola colonna di A.
  11. */
  12.  
  13.  
  14. #include <iostream>
  15. #include <cstdlib>
  16. #include <ctime>
  17.  
  18. const int DIM1 = 5, DIM2 = 6;
  19.  
  20. using namespace std;
  21.  
  22. void metodo(string matrix[][DIM2]);
  23.  
  24. int main()
  25. {
  26.     srand(time(NULL));
  27.  
  28.     string matrix[DIM1][DIM2];
  29.  
  30.     cout << "MATRICE NON RIORDINATA:" << endl;
  31.     for(int x = 0; x < DIM1; x++){
  32.         for(int y = 0; y < DIM2; y++){
  33.             int k = rand()%5+3;
  34.             for(int i = 0; i < k; i++){
  35.                 matrix[x][y] += char(rand()%4+97);
  36.             }
  37.             cout << matrix[x][y] << " ";
  38.         }
  39.         cout << endl;
  40.     }
  41.  
  42.     metodo(matrix);
  43.  
  44.     cout << "\nMATRICE RIORDINATA:" << endl;
  45.     for(int x = 0; x < DIM1; x++){
  46.         for(int y = 0; y < DIM2; y++){
  47.             cout << matrix[x][y] << " ";
  48.         }
  49.         cout << endl;
  50.     }
  51.     return 0;
  52. }
  53.  
  54. void metodo(string matrix[][DIM2]){
  55.     for(int y = 0; y < DIM2; y++){
  56.         for(int x = 0; x < DIM1-1; x++){
  57.             for(int p = x + 1; p < DIM1; p++){
  58.                 if(matrix[x][y] > matrix[p][y]){
  59.                     string s = matrix[x][y];
  60.                     matrix[x][y] = matrix[p][y];
  61.                     matrix[p][y] = s;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement