printing a pascal triangle in C -
01 void pascal(int n){ 02 int i,j; 03 int a[100], b[100]; 04 a[0]= 1; 05 06 for(i = 0; <= n; i++){ 07 printf(" "); 08 b[i]=1; 09 for(j = 0; j <= i; j++){ 10 if (j <= 1) a[j-1]=0; 11 b[j] = a[j-1] + a[j]; 12 printf("%d", b[j]); 13 } 14 (j = 0; j <= i; j++){ 15 a[j] = b[j]; 16 } 17 printf("%d \n"); 18 } 19 20 }
this function i've been trying build, please tell me what's wrong it.
in line-11 using -1 index.check first. check code--
void pascal(int n){ int i,j; int a[100]={0}, b[100]={0}; a[1]= 1; for(i = 1; <= n; i++){ printf(" "); b[i]=1; for(j = 1; j <= i; j++){ if (j <= 1) a[j-1]=0; b[j] = a[j-1] + a[j]; printf("%d ", b[j]); } (j = 1; j <= i; j++){ a[j] = b[j]; } printf("\n"); } }
i have started indexing 1.
c pascals-triangle
No comments:
Post a Comment