Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int ncr(int n,int r);
  6. int fact(int n);
  7.  
  8. int main(void)
  9. { int row,n, i =0, j=0;
  10. int** array; // can't be sure.
  11.  
  12.  
  13. printf("Please enter the (integer) number of rows of pascals triangle to be calculated: ");
  14. scanf("%d", &row); //working
  15.  
  16. array = (int**)malloc(row*sizeof(int*)); // not a clue
  17. for(i=0; i<=row; i++)
  18. array[i] = (int*)malloc(row*sizeof(int)); // not a clue
  19.  
  20. printf("\nBelow are the first %d rows of Pascals Triangle:\n", row); //working
  21.  
  22. i=0;
  23.  
  24.  
  25. for( i=0; i<row; i++)
  26. {
  27. for (j=0; j<row; j++)
  28. {
  29. array[i][j] = ncr(i,j);
  30. array[0][0]=1, array[0][1] =0;
  31. printf("%d ", array[i][j]);
  32. }
  33. printf("\n");
  34. }
  35.  
  36.  
  37.  
  38. free((void*) array);
  39.  
  40. system("pause");
  41. return 0;
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. int ncr(int n, int r)
  51. {
  52. int nfact, nrfact, rfact;
  53. int ans;
  54.  
  55. nfact = fact(n);
  56. nrfact = fact((n-r));
  57. rfact = fact(r);
  58. ans = (nfact)/(rfact*nrfact);
  59. return ans;
  60. }
  61.  
  62.  
  63.  
  64.  
  65. int fact(int n)
  66. {
  67. int i, m=1;
  68. i=1;
  69. m=1;
  70. for(i=1; i<=n ;i++)
  71. {m=m*i;
  72. //printf("%d\n", m);
  73. }
  74.  
  75. return m;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement