Advertisement
Guest User

zad8

a guest
Nov 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <array>
  5. using IntMatrix = std::vector<std::vector<int>>;
  6. int randomInt(int min, int max) {
  7.     static std::default_random_engine e{};
  8.     std::uniform_int_distribution<int> d(min, max);
  9.     return d(e);
  10. }
  11. IntMatrix createMatrix(std::array<unsigned int,2> shape) {
  12.     std::vector<std::vector<int>> macierz (shape[0], std::vector<int>(shape[1]));
  13.     for (int i=0; i<shape[0]; i++){
  14.         for(int j=0; j<shape[1];j++){ macierz[i][j] = 0;}
  15.     }
  16. return macierz;
  17. }
  18.  
  19. IntMatrix randomMatrix(std::array<unsigned int,2> shape,int min,int max) {
  20.     std::vector<std::vector<int>> macierz(shape[0], std::vector<int>(shape[1]));
  21. for (int i=0; i<shape[0];i++){
  22.     for(int j=0; j<shape[0];j++){
  23.         macierz[i][j] = randomInt(min,max);
  24.     }
  25. }
  26. return macierz;
  27. }
  28.  
  29. void print(std::vector<std::vector<int>> macierz){
  30.     for(int i=0;i<macierz.size();i++){
  31.         for(int j=0; j < macierz[0].size(); j++){
  32.             std::cout<< macierz[i][j];
  33.             if (j < macierz[0].size() - 1) std::cout << "  ";
  34.         }
  35.         if (i<macierz[0].size() -1) std::cout << std::endl;
  36.     }
  37.  
  38.  
  39. }
  40.  
  41.  
  42.  
  43. int main(){
  44.     std::array<unsigned int,2> macierz;
  45.     unsigned int n,m;
  46.     int min, max;
  47. std::cout<<"podaj wymiary macierzy"<<std::endl;
  48.  
  49.     macierz[0]=n;
  50.     macierz[1]=m;
  51.     std::cout << "ilosc wierszy:" <<std::endl;
  52.     std::cin >> n;
  53.     std::cout << "ilosc kolumn:" <<std::endl;
  54.     std::cin >> m;
  55.    print(createMatrix(macierz));
  56.      std::cout << "podaj wymiary macierzy losowej";
  57.     std::cout << "ilosc wierszy:" <<std::endl;
  58.     std::cin >> n;
  59.     std::cout << "ilosc kolumn:" <<std::endl;
  60.     std::cin >> m;
  61. std::cout<< "podaj zakres liczb losowych" << std::endl;
  62.     std::cout<< "od" << std::endl;
  63. std::cin>> min;
  64.     std::cout<< "do" << std::endl;
  65.     std::cin>> max;
  66.     print(randomMatrix(macierz,min,max));
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement