Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- template <typename Tipel>
- Tipel** Grbava (Tipel niz[], int br_el)
- {
- for (int i=0; i<br_el; i++) {
- if (niz[i]<0)
- throw "Broj elemenata niza mora biti pozitivan.";
- }
- Tipel** pok=nullptr;
- try{ pok=new Tipel *[br_el];
- for (int i=0; i<br_el; i++) pok[i]=nullptr;
- for (int i=0; i<br_el; i++) {
- pok[i]= new Tipel[niz[i]];
- for (int j=0; j<niz[i]; j++)
- pok[i][j]=Tipel ();
- }
- return pok;
- }
- catch(bad_alloc) {
- throw "Greska prilikom alokacije.";
- }
- }
- int main () {
- int niz[5]={3,5,2,-1,6};
- int **mat (nullptr);
- try {mat=Grbava(niz,5);
- for (int i=0; i<5; i++) {
- for (int j=0; j<niz[i]; j++) cout<<mat[i][j];
- cout<<endl;
- }
- if (!mat) {
- for (int i=0; i<5; i++)
- delete [] mat[i];
- delete [] mat;
- }
- }
- catch(const char poruka[]) {
- cout<<"Greska: "<<poruka;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement