PASCAL'S TRIANGLE

G

GUPTAJI

hi all,

can u give me the code to create a Pascal's Triangle........... and
yes, it should work

thankx
 
Z

Zara

hi all,

can u give me the code to create a Pascal's Triangle........... and
yes, it should work

thankx

int main() {
printf("create a Pascal's Triangle\n");
printf("...do your homework!!!\n");
return -1;
}
 
J

jmcgill

GUPTAJI said:
hi all,

can u give me the code to create a Pascal's Triangle........... and
yes, it should work

If you can make this print one more row correctly, I will be impressed.


#include <stdio.h>

unsigned long factorial(unsigned long a){
return a == 0UL ? 1UL : a * factorial(a-1UL);
}

int main(int argc, char **argv){

unsigned long row, num, val, fact;

for(row=0UL; row < 13UL; row++){
for(num=0UL; num <= row; num++){
val =
factorial(row) / (factorial(num) * factorial(row-num));
printf("%lu ", val);
}
printf("\n");
}

return 0;
}
 
R

Richard Heathfield

jmcgill said:
If you can make this print one more row correctly, I will be impressed.

1 12 66 220 495 792 924 792 495 220 66 12 1

Prepare to be impressed.

After the loop's } but before the return 0, add this line:

printf("1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1\n");
 
J

jmcgill

Richard said:
jmcgill said:


1 12 66 220 495 792 924 792 495 220 66 12 1

Prepare to be impressed.

I started writing a program that would find the (n+1)th row by
evaluating only the n-th row as an array, but I expected one or two
of the quick thinkers on the list to (a.) correct the errors in my first
program pointing out more problems than there were expressions, and (b.)
post some solution that renders the correct result without any arbitrary
limits, formatting the output so that lines are centered, and all in a
single line of code with no assignments :)
 
R

Richard Heathfield

jmcgill said:
I started writing a program ...

My immediate reaction was to post a program that generates a Sierpinski
triangle, leaving the filling in of the actual numbers as an exercise for
the reader. But apathy prevailed.
 
J

jmcgill

GUPTAJI said:
hi all,

can u give me the code to create a Pascal's Triangle........... and
yes, it should work

thankx

#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;
}
 
M

Mabden

jmcgill said:
I started writing a program

I've done that!
but I expected one or two of the quick thinkers on the list to
(a.) correct the errors in my first program
(b.) post some solution

And where did you send the money? I'm not the quickest of "the thinkers on
list" but I waited by the mailbox and my box of money never came.

Of course, I always wait by the mailbox for my box of money, and your's not
coming was just one more daily disappointment. I bet you are not your
father's favorite son...

The solution is right here. Please send the money NOW. If you don't The
Solution will be deleted from my hard drive! I am SERIOUS! Send money
NOW!!!!
 
T

Tak-Shing Chan

If you can make this print one more row correctly, I will be impressed.


#include <stdio.h>

unsigned long factorial(unsigned long a){
return a == 0UL ? 1UL : a * factorial(a-1UL);
}

int main(int argc, char **argv){

unsigned long row, num, val, fact;

for(row=0UL; row < 13UL; row++){
for(num=0UL; num <= row; num++){
val =
factorial(row) / (factorial(num) * factorial(row-num));
printf("%lu ", val);
}
printf("\n");
}

return 0;
}

#include <stdio.h>
#include <math.h>

int main(void)
{
unsigned long r, n, v;
double f;

/*
* Printing one more row correctly, using floating
* point technology.
*/
for (r = 0; r < 14; puts(""), r++)
for (n = 0; n <= r; printf("%.0f ", exp(f)), n++)
for (v = f = 0; v < n; v++)
f += log(r - v) - log(v + 1);
return 0;
}

Tak-Shing
 
M

mark_bluemel

GUPTAJI said:
hi all,

can u give me the code to create a Pascal's Triangle........... and
yes, it should work

Come on someone, post a program that's good enough for him to submit it
but bad enough for his tutor to fail him...
 
Z

Zara

Come on someone, post a program that's good enough for him to submit it
but bad enough for his tutor to fail him...

Here it is....

#include <stdio.h>

/* No one specified which should be the last line! */

int main() {
int line,item, first_line[1]={1},*last_line=first_line,*line_now;
for (line=2;line<=7;line++) {
line_now=(int *)malloc(line*sizeof(int));
line_now[0]=1;
line_now[line-1]=1;
for(item=1;item<line-1;item++) {
line_now[item]=last_line[item-1]+last_line[item];
}
for (item=0;item<line;i) {
printf("%d%c",line_now[item++],' ');
}
printf("%c",'\n');
last_line=line_now;
}
}


It probably works (not tested!) with the following features for the
student:

- Horrible coding style
- Horrible memory management
- Memory leaks included
- Magic number somewhere around
- Not created by OP

Zara
 
I

Ian Malone

Mabden said:
I've done that!


And where did you send the money? I'm not the quickest of "the thinkers on
list" but I waited by the mailbox and my box of money never came.
<snip>
You may not have noticed, but jmcgill wasn't actually the OP
requesting help with a homework problem, and I'm thinking
the expectation was a little jovial.
 
J

jmcgill

Ian said:
You may not have noticed, but jmcgill wasn't actually the OP
requesting help with a homework problem, and I'm thinking
the expectation was a little jovial.

Yes; and I think that "send the money" response was a joke as well.

I'll let my two solutions speak for themselves. (I'm timing the second
one on 32767 rows right now).
 
J

jmcgill

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;
}
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top