我想用二级指针写一个杨辉三角输出到文件data,txt 可是总是在第7行出现差错: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 6 5 6 15 20 15 11 1 1 11 21 35 35 26 12 1 1 15 72 204 418 675 905 1055 1120 1 13 44 88 126 131 99 51 14 1 1 15 72 204 418 675 905 1055 1120 1135 1136 1 15 72 204 418 675 905 1055 1120 1135 1136 1 以下是程序原文件: #include <stdio.h> #include <stdlib.h>
int main() { int n; int yang(int); clrscr(); printf("input the lines of yanghui sanjia"); scanf("%d",&n); yang(n); return 1; }
int yang(int num) { FILE *pf; long far **y; int i,j,k,m,n,l; y = (long**)malloc( num*sizeof(long **) ); if (NULL==y) { printf("\nerror!\n"); getch(); exit(0); } for (l = 0 ; l<num ; l++) { *(y+l) = (long *)malloc( (l+1)*sizeof(long *) ); if (NULL==*y) { printf("\nerror\n"); getch(); exit(0); } } for (i = 0 ; i<num ; i++) { y[i][0] = 1; y[i][i] = 1; } for (j = 2 ; j<num ; j++) for (k = 1 ; k<j ; k++) { i++; y[j][k]=y[j-1][k-1]+y[j-1][k]; i--; } pf = fopen ( "data.txt","w" );
for (m = 0 ; m<num ; m++) { for (n = 0 ; n<=m ;n++) fprintf(pf,"%5ld ",y[m][n]); fprintf(pf,"\n"); } fclose(pf); return 1; }