Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int **Pascal (int n) {
- if (n<=0) throw "Pogresan parametar.";
- int **mat=nullptr;
- int br_elem{0};
- for (int i=0; i<n; i++) br_elem+=i+1;
- try {
- mat= new int*[n];
- mat[0]=new int [br_elem];
- for (int i=1; i<=n; i++)
- mat[i]=mat[i-1]+i;
- for (int i=0; i<n; i++)
- for (int j=0; j<=i; j++)
- { if ((j==0) || (j==i)) mat[i][j]=1;
- else mat[i][j]=mat[i-1][j-1]+mat[i-1][j];
- }
- return mat;
- }
- catch(...)
- {
- throw "Kreiranje nije uspjelo.";
- }
- }
- int main () {
- int n;
- cout<<"Unesite broj n: ";
- cin>>n;
- cin.clear();
- cin.ignore(1000,'\n');
- cout<<endl;
- int **matrica=nullptr;
- try {
- matrica=Pascal(n);
- for (int i=0; i<n; i++) {
- for (int j=0; j<=i; j++)
- cout<<matrica[i][j]<<" ";
- cout<<endl;
- }
- if (matrica!=nullptr) {
- delete [] matrica[0];
- delete [] matrica;
- }
- }
- catch(const char poruka[]) {
- cout<<poruka;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement