Advertisement
sellmmaahh

popravni-sept-2014-zad8-Alokacija

Jul 30th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <algorithm>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. template<typename TipPok, typename TipEl>
  9. void Alociraj (TipPok &a, int m, int n, TipEl v, bool mod ) {
  10.  
  11.     if (!mod) {
  12. try {
  13.     TipPok mat=nullptr;
  14.     mat=new TipEl *[m];
  15.     for (int i=0; i<m; i++)
  16.         mat[i]=nullptr;
  17.  
  18.     for (int i=0; i<m; i++)
  19.      mat[i]=new TipEl [n];
  20.  
  21.     for (int i=0; i<m; i++) {
  22.         for (int j=0; j<n; j++)
  23.             mat[i][j]=v;
  24.     }
  25.     a=mat;
  26.  
  27. }
  28.  catch(bad_alloc) {
  29. throw "Greska prilikom alokacije.";
  30. }}
  31. else {
  32.     try { TipPok mat=nullptr;
  33.         mat=new TipEl  *[m];
  34.         mat[0]=new TipEl [m*n];
  35.         for (int i=1; i<=m; i++) mat[i]=mat[i-1]+n;
  36.  
  37.     for (int i=0; i<m; i++) {
  38.         for (int j=0; j<n; j++)
  39.         mat[i][j]=v;
  40.     }
  41.     a= mat;
  42.     }
  43.     catch(bad_alloc) {
  44. throw "Greska prilikom alokacije.";
  45. }}
  46. }
  47.  
  48. int main () {
  49.     try {
  50.         int **pok;
  51.         Alociraj(pok,3,3,0, true);
  52.  
  53.     for (int i=0; i<3; i++) {
  54.         for (int j=0; j<3; j++)
  55.         cout<<pok[i][j]<<" ";
  56.     cout<<endl;
  57.     }
  58.  
  59.     delete [] pok[0];
  60.     delete[] pok;
  61.     }
  62.     catch (const char poruka[]) {
  63.     cout<<poruka;
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement