C++: Pascal's triangle - weird results -


this first question don't mad @ me, if did wrong. have make c++ program returns element selected row, example:

triangle 4 0 1 2 3 

should return elements: 0, 1, 2 , 3 row number 4, returns strange things, like:

element 0: 1 element 1: 10179988 element 2: 50792126 element 3: 91425820 

i have no idea why
here's code:

#include <cstdlib> #include <iostream> #include <string> #include <cstring> using namespace std;  class pascal {     private:         int *tab;      public:         pascal(int n) throw(string) {             if (n < 0)                 throw (string)"";              tab = new int[n+1];              for(int = 1; <= n; i++) {                 for(int k = i; k >=0; k--) {                     if (k - 1 >= 0)                         tab[k] += tab[k-1];                     else                         tab[k] = 1;                 }             }         };      int element(int m) {         return tab[m];     } };  int main(int argc, char* argv[]) {         int n = 0, m = 0, elem = 0;          try {             n = strtol(argv[1], null, 0);             pascal *row;              for(int = 2; < argc; i++) {                 try {                     m = strtol(argv[i], null, 0);                     row = new pascal(n+1);                      if (m <= n && m >= 0) {                         elem = row->element(m);                         cout << "element " << m << ": "<< elem << endl;                     } else                         cout << m << " - bad element index" << endl;                  } catch (string ex) {                     cout << argv[i] << " - bad element index!" << endl;                     continue;                 }                  delete[] row;             }         } catch (string e) {             cout << argv[1] << " - bad row index!" << endl;             return 0;         }     } 

i'll grateful answer

try

tab = new int[n+1];  for(int = 0; <= n; i++) {     tab[i] = 1;     for(int k = i; --k > 0; )         tab[k] += tab[k-1]; } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -