Mabden said:
I've done that!
And where did you send the money?
I sent it to "Mabden, General Delivery, Tardville", but it came back
"return to sender". Oh well.
The solution is right here.
Obviously you missed mine, so here it is again. If you truly have a
solution of your own, please post it and I won't put you in my killfile
as an insulting troll.
Now, what I wish I could figure out a way to do, is to calculate the
line length of the last row, so that the rows can be centered in a
single pass. Is there an arithmetic method to determine the number of
digits for each number in the Nth row of a triangle (given base 10),
without computing N rows? Or for that matter, is there a method to
compute an arbitrary row that does not require computing a factorial
that quickly grows beyond the limits standard numeric types? (That's a
lot of work just to format rows of numbers).
The "easy way" to format it seems to simply be to store each row, and
then evaluate the last row to get the maximum length, and then use that
value to format all rows. But is there a way, knowing that you will be
printing N rows, to format rows 1 through N-1 based on the projected
length of the Nth row, other than by computing all rows 1 through N
first?
#include <stdio.h>
#include <stdlib.h>
void print_row(unsigned long *row){
unsigned long v;
while( 0UL != (v = *row++)){
printf("%lu ", v);
}
printf("\n");
}
int card_row(unsigned long *row){
int i;
i=0;
while( 0UL != *row++){
i++;
}
return i;
}
unsigned long * next_row(unsigned long * row){
unsigned long * retval;
int count, i;
count = card_row(row);
retval = malloc( (count+2) * sizeof(unsigned long));
*(retval+0) = 1UL;
*(retval+count+1) = 0UL;
for(i=1; i<=count; i++){
*(retval + i) = *(row + i -1) + *(row + i);
}
return retval;
}
int main(int argc, char **argv){
int c;
unsigned long *row_cur, *row_next;
row_cur = malloc(2 * sizeof(unsigned long));
*row_cur = 1UL; *(row_cur+1) = 0UL;
print_row(row_cur);
for( c= 0; c< 20; c++){
row_next = next_row(row_cur);
print_row(row_next);
free(row_cur);
row_cur = row_next;
}
free(row_cur);
return 0;
}