Pascal's triangle code - Please help!

L

LL

I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below code
no longer does Pascal's triangle as the portion of the code is commented
out. Please explain to me the commented out portion. I seem to
understand the recursive logic of it, but don't understand something
else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
 
L

LL

LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it.
That someone is Jens Thoms Toerring. It would be good if he's still around.


The below code
no longer does Pascal's triangle as the portion of the code is commented
out. Please explain to me the commented out portion. I seem to
understand the recursive logic of it, but don't understand something
else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
 
J

Jens Thoms Toerring

LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below code
no longer does Pascal's triangle as the portion of the code is commented
out. Please explain to me the commented out portion. I seem to
understand the recursive logic of it, but don't understand something
else. Thanks.

It's more fun obfuscating it than the other way round;-) But
here we go:

#include <stdio.h>
#include <stdlib.h>

void pt( long order, long *list )
{
long tmp = 1;
long index;

if ( order > 0 )
{
/* Print all lower order rows and set up list in the process
so we end up with the values in list set to the elements of
the row of order 'order - 1' (execpt the 1 at the end) */

pt( order - 1, list );

/* Print out the 1 that starts all rows */

printf( "1 " );

/* list now contains the elements for order - 1 (except the 1 at
the end. E.g for for order == 10 it contains
1, 9, 36, 84, 126, 126, 84, 36, 9
while tmp is 1. Calculate and print all elements of the row
(except the 1 at the start and the end). Note that we go
backwards through list but that's ok since it's symmetric. */

index = order - 1;
while ( index > 0 )
{
/* tmp here is 1 the first time round and equal to
list[ index ] afterwards */

list[ index ] = list[ index - 1 ] + tmp;
printf( "%ld ", list[ index ] );
index -= 1;
tmp = list[ index ];

/* This could also be written as

if ( index == order - 1 )
list[ index ] = 1 + list[ index - 1 ];
else
list[ index ] += list[ index - 1 ];
printf( "%ld ", list[ index ] );
index -= 1;

so you could get rid of the tmp variable */
}
}

/* Print out the 1 that ends all rows (in case of order == 1 that's
all that get done in the function) */

puts( "1" );
}

int main( void )
{
long order = 10, *list;

list = malloc( order * sizeof * list );

list[ 0 ] = 1; /* set up list for case of order == 0 */
pt( order, list );
free( list );
return 0;
}


Does it now make more sense to you? All I did was give variables
some more reasonable names, take apart overly terse constructs
and add a few comments. Otherwise it's unchanged.

Regards, Jens
 
L

LL

LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below code
no longer does Pascal's triangle as the portion of the code is commented
out. Please explain to me the commented out portion. I seem to
understand the recursive logic of it, but don't understand something
else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
Thanks for Jen's code in the other thread. I'll comment on it soon.

To aid my understanding I've written down the values of l[o] for values
of o(I've omitted to say explicitly for decrementing values of o):

o=0:
(1) // puts("1");

o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");

where is the case 1 1?

o=2:
(1)
l[2]=l[1]+t=l[1]+l[0]=2+1=3
l[1]=l[0]+t=l[0]+l[1]=1+2=3
(1)

o=3:
(1)
l[3]=l[2]+t=l[2]+l[0]=3+1=4
l[2]=l[1]+t=l[1]+l[2]=3+3=6
l[1]=l[0]+t=l[0]+l[1]=1+3=4
(1)

I haven't written for o>3. Is my description correct besides the missing
1 1 case? Where is the 1 1 case? I must have interpreted the code
incorrectly. Any ideas where I've gone wrong?
 
L

LL

LL said:
LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below code
no longer does Pascal's triangle as the portion of the code is
commented out. Please explain to me the commented out portion. I seem
to understand the recursive logic of it, but don't understand
something else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
Thanks for Jen's code in the other thread. I'll comment on it soon.

To aid my understanding I've written down the values of l[o] for values
of o(I've omitted to say explicitly for decrementing values of o):

o=0:
(1) // puts("1");

o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");

where is the case 1 1?

o=2:
(1)
l[2]=l[1]+t=l[1]+l[0]=2+1=3
l[1]=l[0]+t=l[0]+l[1]=1+2=3
(1)

o=3:
(1)
l[3]=l[2]+t=l[2]+l[0]=3+1=4
l[2]=l[1]+t=l[1]+l[2]=3+3=6
l[1]=l[0]+t=l[0]+l[1]=1+3=4
(1)

I haven't written for o>3. Is my description correct besides the missing
1 1 case? Where is the 1 1 case? I must have interpreted the code
incorrectly. Any ideas where I've gone wrong?
pt(1,l) seems to be the 1 1 case so all of my cases should be for cases
one greater but i don't understand.
 
L

LL

LL said:
LL said:
LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below
code no longer does Pascal's triangle as the portion of the code is
commented out. Please explain to me the commented out portion. I seem
to understand the recursive logic of it, but don't understand
something else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
Thanks for Jen's code in the other thread. I'll comment on it soon.

To aid my understanding I've written down the values of l[o] for
values of o(I've omitted to say explicitly for decrementing values of o):

o=0:
(1) // puts("1");

o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");

where is the case 1 1?

o=2:
(1)
l[2]=l[1]+t=l[1]+l[0]=2+1=3
l[1]=l[0]+t=l[0]+l[1]=1+2=3
(1)

o=3:
(1)
l[3]=l[2]+t=l[2]+l[0]=3+1=4
l[2]=l[1]+t=l[1]+l[2]=3+3=6
l[1]=l[0]+t=l[0]+l[1]=1+3=4
(1)

I haven't written for o>3. Is my description correct besides the
missing 1 1 case? Where is the 1 1 case? I must have interpreted the
code incorrectly. Any ideas where I've gone wrong?
pt(1,l) seems to be the 1 1 case so all of my cases should be for cases
one greater but i don't understand.
What I mean is ... for cases one greater except o=0 that was correct.
 
L

LL

LL said:
LL said:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below code
no longer does Pascal's triangle as the portion of the code is
commented out. Please explain to me the commented out portion. I seem
to understand the recursive logic of it, but don't understand
something else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
Thanks for Jen's code in the other thread. I'll comment on it soon.

To aid my understanding I've written down the values of l[o] for values
of o(I've omitted to say explicitly for decrementing values of o):

o=0:
(1) // puts("1");

o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");

where is the case 1 1?

o=2:
(1)
l[2]=l[1]+t=l[1]+l[0]=2+1=3
l[1]=l[0]+t=l[0]+l[1]=1+2=3
(1)

o=3:
(1)
l[3]=l[2]+t=l[2]+l[0]=3+1=4
l[2]=l[1]+t=l[1]+l[2]=3+3=6
l[1]=l[0]+t=l[0]+l[1]=1+3=4
(1)

I haven't written for o>3. Is my description correct besides the missing
1 1 case? Where is the 1 1 case? I must have interpreted the code
incorrectly. Any ideas where I've gone wrong?
#include <stdio.h>

void pt(long o) {
if (o) {
for (;o;--o) {
printf("%ld ", o);
}
}
}

int main() {
pt(1); // 1
}

???
 
L

LL

LL said:
LL said:
LL said:
LL wrote:
I've cut down the code for Pascal's triangle that someone in this
channel posted few weeks ago in order to understand it. The below
code no longer does Pascal's triangle as the portion of the code is
commented out. Please explain to me the commented out portion. I
seem to understand the recursive logic of it, but don't understand
something else. Thanks.

#include <stdio.h>
#include <stdlib.h>

void pt( long o, long *l ) {
// How does this work? Please explain.
/*
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
*/

// I don't know how l[o] is non-zero for o>0 when they are null.
for (;o;--o) {
printf("%ld", l[o-1]); // 0000000001
}
}

int main() {
long o=10, *l;

l=malloc(o*sizeof(*l));

// l[0]=1
*l = 1;
pt(o,l);
free( l );
return 0;
}
Thanks for Jen's code in the other thread. I'll comment on it soon.

To aid my understanding I've written down the values of l[o] for
values of o(I've omitted to say explicitly for decrementing values of
o):

o=0:
(1) // puts("1");

o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");

where is the case 1 1?

o=2:
(1)
l[2]=l[1]+t=l[1]+l[0]=2+1=3
l[1]=l[0]+t=l[0]+l[1]=1+2=3
(1)

o=3:
(1)
l[3]=l[2]+t=l[2]+l[0]=3+1=4
l[2]=l[1]+t=l[1]+l[2]=3+3=6
l[1]=l[0]+t=l[0]+l[1]=1+3=4
(1)

I haven't written for o>3. Is my description correct besides the
missing 1 1 case? Where is the 1 1 case? I must have interpreted the
code incorrectly. Any ideas where I've gone wrong?
pt(1,l) seems to be the 1 1 case so all of my cases should be for
cases one greater but i don't understand.
What I mean is ... for cases one greater except o=0 that was correct.
Actually I was correct for all cases. The 1 1 cases occur even when o=0.

<0>1
1[0] <0>1
1[1] 2[1] <0>1
1[2] 3[2] 3[1] <0>1
1[3] 4[3] 6[2] 4[1] <0>1
1[4] 5[4] 10[3] 10[2] 5[1] <0>1
1[5] 6[5] 15[4] 20[3] 15[2] 6[1] <0>1
1[6] 7[6] 21[5] 35[4] 35[3] 21[2] 7[1] <0>1
1[7] 8[7] 28[6] 56[5] 70[4] 56[3] 28[2] 8[1] <0>1
1[8] 9[8] 36[7] 84[6] 126[5] 126[4] 84[3] 36[2] 9[1] <0>1
1[9] 10[9] 45[8] 120[7] 210[6] 252[5] 210[4] 120[3] 45[2] 10[1] <0>
 
J

Jens Thoms Toerring

LL said:
To aid my understanding I've written down the values of l[o] for values
of o(I've omitted to say explicitly for decrementing values of o):

Haven't you seen my reply with the "do-obfuscated" code I
posted a few days ago?
o=0:
(1) // puts("1");
o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");
where is the case 1 1?

Because that's not correct. For o == 1 you get

(1) // from printf("1 ") in for loop initialization
(1) // from puts("1")

The body of the for loop is never executed for the case of 1
because o is decremented to 0 in the call of pt() in the for
loops initialization.

Then for o == 2 you have

(1) // from printf("1 ") in for loop initialization
(2) // = l[1] = l[0] + t = 1 + 1, printed in the body of the for look
(1) // from puts("1")

etc.

And, please stop posting dozends of posts just adding a
line or two, but citing all of the previous ones. Thanks.

Regards, Jens
 
L

LL

Jens said:
LL said:
To aid my understanding I've written down the values of l[o] for values
of o(I've omitted to say explicitly for decrementing values of o):

Haven't you seen my reply with the "do-obfuscated" code I
posted a few days ago?
o=0:
(1) // puts("1");
o=1:
(1)
l[1]=l[0]+t=1+1=2
(1) // printf("1");
where is the case 1 1?

Because that's not correct. For o == 1 you get

(1) // from printf("1 ") in for loop initialization
(1) // from puts("1")

The body of the for loop is never executed for the case of 1
because o is decremented to 0 in the call of pt() in the for
loops initialization.
Wow thanks.
Then for o == 2 you have

(1) // from printf("1 ") in for loop initialization
(2) // = l[1] = l[0] + t = 1 + 1, printed in the body of the for look
(1) // from puts("1")

etc.

And, please stop posting dozends of posts just adding a
line or two, but citing all of the previous ones. Thanks.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top