int pascal(int r, int c) { if (c==0 || c==r) { return 1; } else { return pascal(r-1,c-1) + pascal(r-1,c); } }
int main(int argc, char *argv[]){ int n, **res, i, j; if(argc <= 1){ printf("Error with arguments <= 1"); return 0; } else if(argc == 2){ n = atoi(argv[1]); res = pascal(n); if(res == NULL){ printf("Error"); return 0; } } else{ printf("Error with arguments >2"); return 0; } for(i = 0; i < n; i++){ for(j = 0; j <= i; j++){ printf("%d ", res[i][j]); } printf("\n"); } for(i = 0; i < n; i++){ free(res[i]); } free(res); return 0; }
int main(){ int order; printf("Enter the order\n"); scanf("%d",&order); linear(order); even_odd_switch(order); pascal(order); }
vector<int> getRow(int rowIndex) { vector <int> pascal(1,1); for(int i=1;i<=rowIndex;i++){ pascal.push_back(1); for(int k=i-1;k>0;k--){ pascal[k]=pascal[k]+pascal[k-1]; } } return pascal; }
vector<vector<int>> generate(int numRows) { vector<vector<int>> pascal(numRows,(vector<int>(1,1))); for(int i=1;i<numRows;i++) { for(int j=0;j<pascal[i-1].size()-1;j++) { pascal[i].push_back(pascal[i-1][j] + pascal[i-1][j+1]); } pascal[i].push_back(1); } return pascal; }
int main() { int a = 11, b = 4, mid = 40, top = 151; int x, y, z; pascal(a, b, mid, top, &x, &y, &z); if(x != 0) printf("x: %d, y: %d, z: %d\n", x, y, z); else printf("No solution\n"); return 0; }
// Function to create processes. It does not return. _Noreturn void create_procs(void) { // left[0], left[1] -- read and write end of the pipe leading to the // previous process int left[2]; for(int i = 0; i < n; ++i) { int pipes_to[2], pipes_from[2], right[2]; if(-1 == pipe(pipes_to) || -1 == pipe(pipes_from)) syserr("Creating processes failed: Unable to create pipe"); pid_t pid; switch(pid = fork()) { case -1: syserr("Creating processes failed: Unable to fork"); case 0: if(-1 == close(pipes_to[1]) || -1 == close(pipes_from[0])) syserr("Creating processes failed: Unable to close"); if(i > 0 && (-1 == close(left[0]) || -1 == close(left[1]))) syserr("Creating processes failed: Unable to close"); left[0] = pipes_to[0]; left[1] = pipes_from[1]; break; // Loop to create the next process default: // Run the i-th process right[0] = pipes_from[0]; right[1] = pipes_to[1]; if(-1 == close(pipes_to[0]) || -1 == close(pipes_from[1])) syserr("Creating processes failed: Unable to close"); if(i == 0) pascal(i, NULL, right); else worker(i, left, right); if(pid != waitpid(pid, NULL, 0)) syserr("Ending processes failed: Unable to wait"); exit(0); } } // Run the last process worker(n, left, NULL); exit(0); }
int main() { int i, j; int C[N][N]; pascal(C); // Affichage du résultat for(i = 0; i < N; ++i) { for(j = 0; j < N; ++j) { if(C[i][j] != 0) printf("%d ", C[i][j]); } printf("\n"); } return 0; }
int main(int argc, char **argv) { int i = 0; int n = 0; printf("Rows? "); scanf("%d", &n); for (i = 0; i < n; i++) { for (int j = 0; j < (n - i); j++) printf(" "); pascal(i); } return 0; }
vector<vector<int> > Solution::generate(int A) { vector<vector<int> > pascal(A); int i,j; for (i = 0; i < A; i++) { pascal[i].resize(i + 1); } for (i = 0; i < A; i++) { for ( j = 0; j < i + 1; j++) { if (j == 0 || i == j ) { pascal[i][j] = 1; } else { pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]; } } } return pascal; }
int main() { int i,j,k1,k2,n; scanf("%d",&n); i=j=n; for(i=0;i<n;i++) { k2=n-i-1; for(k1=0;k1<k2;k1++) printf(" "); for(j=0;j<=i;j++) { printf("%d",pascal(i,j)); if(j<i) printf("*"); } printf("\n"); } return 0; }
poly * poly_from_relation_number(uint32_t rn, int rank) { poly *p; int i; int j; int num_rows; assert(VALID_RANK(rank)); if ((p = malloc(sizeof(poly))) == NULL) { perror("malloc"); return NULL; } if ((p->coeffs = malloc(sizeof(int) * (rank + 1))) == NULL) { perror("malloc"); free(p); return NULL; } p->degree = rank; /* Zero-out the coefficients. */ memset(p->coeffs, 0, sizeof(int) * (rank + 1)); /* For each possible number of true variables [0,rank] (the function * `q` does exactly that), find the number of rows. Add to the polynomial. * Uses the binomial theorem, since the generated polynomials are * of the form p^x * (1-p)^y, where x + y == rank */ for (i = 0; i <= rank; i++) { if ((num_rows = q(rn, rank, rank - i)) > 0) { for (j = 0; j <= i; j++) { /* Assigns from the top degree. */ *(p->coeffs + rank-j) += pascal(i, j) * num_rows * (j % 2 == i % 2 ? 1 : -1); } } } return p; }
vector<int> getRow(int rowIndex) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> pascal(rowIndex + 1); int lastLine_prev = 0; //Initialization pascal[0] = 1; for(int i = 1; i <= rowIndex; ++i) { //Will invoke size() every time? lastLine_prev = pascal[0]; for(int j = 1; j < i; ++j) { int temp = pascal[j]; pascal[j] = pascal[j] + lastLine_prev; lastLine_prev = temp; } pascal[i] = 1; } return pascal; }
int main() { pascal(8); return 0; }
vector<int> getRow(int rowIndex) { vector<int> pascal(rowIndex+1); for(int i = 0;i<=rowIndex/2;i++) pascal[rowIndex-i] = pascal[i] = combination(i,rowIndex); return pascal; }