Advertisement
naskedvi

S6 - zad.9

Apr 29th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <new>
  3. #include <iomanip>
  4.  
  5.  
  6. template<typename Tip>
  7. void KreirajMatricu(Tip **&mat, int n, int m)
  8. {
  9.     try {
  10.        Tip **mat(new Tip*[n]);
  11.        for(int i=0; i<n; i++)
  12.             mat[i]=nullptr;
  13.        try{
  14.          for(int i=0; i<n; i++)
  15.             mat[i]= new Tip[m];
  16.  
  17.         for(int i = 0; i < n; i++)
  18.         {
  19.             for(int j = 0; j < m; j++)
  20.                mat[i][j]=0;
  21.         }
  22.  
  23.         }
  24.  
  25.     catch(...) {
  26.         for(int i=0; i<n; i++)
  27.         delete[] mat[i];
  28.         delete[] mat;
  29.         throw;
  30.        }
  31.     }
  32.     catch(...) {
  33.         throw "Alokacija nije uspjela!\n";
  34.        }
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40.     double **m(0);
  41.     try {
  42.     KreirajMatricu(m, 5, 3);
  43.  
  44.     for(int i = 0; i < 5; i++)
  45.     {
  46.         for(int j = 0; j < 3; j++)
  47.            std::cout<<m[i][j];
  48.         std::cout<<std::endl;
  49.     }
  50.     }
  51.      catch(const char poruka[]) {
  52.         std::cout << poruka;
  53.         }
  54.  
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement