468,457 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

PASCAL'S TRIANGLE

hi all,

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

thankx

Oct 5 '06 #1
25 5474
On 4 Oct 2006 21:15:20 -0700, "GUPTAJI" <pa************@gmail.com>
wrote:
>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;
}
Oct 5 '06 #2
GUPTAJI wrote:
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;
}
Oct 5 '06 #3
jmcgill said:
GUPTAJI wrote:
>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.
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");

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 5 '06 #4
Richard Heathfield wrote:
jmcgill said:
>GUPTAJI wrote:
>>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.

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 :-)
Oct 5 '06 #5
jmcgill said:
Richard Heathfield wrote:
>jmcgill said:
>>GUPTAJI wrote:
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.

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

Prepare to be impressed.

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 5 '06 #6
GUPTAJI wrote:
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;
}
Oct 5 '06 #7
Why do you do his homework?

You are not really helping him
Oct 5 '06 #8
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:Hj1Vg.755$rS.421@fed1read05...
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!!!!

--
Mabden
Oct 5 '06 #9
On Wed, 4 Oct 2006, jmcgill wrote:
GUPTAJI wrote:
>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;
}
#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
Oct 5 '06 #10

GUPTAJI wrote:
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...

Oct 5 '06 #11
On 5 Oct 2006 06:34:53 -0700, ma**********@pobox.com wrote:
>
GUPTAJI wrote:
>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...
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
Oct 5 '06 #12
Mabden wrote:
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:Hj1Vg.755$rS.421@fed1read05...
>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.
<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.

--
imalone
Oct 5 '06 #13
"GUPTAJI" writes:
can u give me the code to create a Pascal's Triangle........... and
yes, it should work
Try to code something up with the information contained in this link. If
you have a problem, post the code you produced.

http://ptri1.tripod.com/
Oct 5 '06 #14
jacob navia wrote:
Why do you do his homework?

You are not really helping him
I don't care about him. I did the exercise because it helped *me*.
Oct 5 '06 #15

GUPTAJI wrote:
hi all,

can u give me the code
How can we know if U (funny name, isn't it) can give you code?

Oct 5 '06 #16
Ian Malone wrote:
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).
Oct 5 '06 #17
Mabden wrote:
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:Hj1Vg.755$rS.421@fed1read05...
>I started writing a program

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

Oct 5 '06 #18
In article <11**********************@h48g2000cwc.googlegroups .com>,
Ingo Menger <qu*********@consultant.comwrote:
>
GUPTAJI wrote:
>hi all,

can u give me the code

How can we know if U (funny name, isn't it) can give you code?
Didn't he used to be the leader of Vietnam? Or was it the UN?
I can't remember.

Oct 5 '06 #19

"Ingo Menger" <qu*********@consultant.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
GUPTAJI wrote:
>hi all,

can u give me the code

How can we know if U (funny name, isn't it) can give you code?
Well, there was U Thant
>

Oct 5 '06 #20
"Ian Malone" <ib***@cam.ac.ukwrote in message
news:45**************@cam.ac.uk...
Mabden wrote:
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:Hj1Vg.755$rS.421@fed1read05...
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.
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.
I did not bother to notice that but I did notice KISS MY FUCKING ASS!!!

HTH,

--
Mabden
Oct 6 '06 #21
Top post, sorry.

This far too detailed an email to something that _I_ said about this post.
I'm pretty sure nothing I said would have prompted a detailed answer like
this, but if it really was directed to me, Dude, I really am not so much a
troll as someone who pokes fun at bad posts. Usually something, "Oh,
homework, how original. See Valuehack" that kind of stuff. So I'm not sure
why you are so pissed off or why you went to so much trouble to "out-code"
me.

The " Obviously you missed mine" post is accurate. I did. I don't get where
you are coming from. You can explain, but I don't care so it's not really
worth it, but I wouldn't mind a 2 line non-confrontational post saying "you
were a bastard when you said this to my post that said this." Otherwise it's
too much work to figure it out.

But I think the whole "putting-me-in-your-killfile" would be a really Good
Thing. Pussy.
I tend to post things that pop into my mind, and you get upset with that.
I'm glad we had this talk.

- Mabden
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:nM9Vg.765$rS.204@fed1read05...
Mabden wrote:
"jmcgill" <jm*****@email.arizona.eduwrote in message
news:Hj1Vg.755$rS.421@fed1read05...
I started writing a program
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;
}

Oct 6 '06 #22

GUPTAJI wrote:
hi all,

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

thankx
/* I bet my program is the shortest, totally correct answer: */
#include <stdio.h>
int main(void) {puts("1");return 0;}
For a more complete answer, here is a big Pascal's triangle:
http://cap.connx.com/chess-engines/n...oach/pas.c.bz2
that was generated by this program:
http://cap.connx.com/chess-engines/n...pascal.exe.bz2

Sorry, but I can't seem to remember where I put the source code.

Oct 6 '06 #23
Mabden wrote:
The " Obviously you missed mine" post is accurate. I did. I don't get where
you are coming from. You can explain, but I don't care so it's not really
worth it, but I wouldn't mind a 2 line non-confrontational post
There is no way I'm going to take a non-confrontational approach with
anyone who sinks to the level of personal insults. Why would I do that?

Oct 6 '06 #24
jmcgill said:
Mabden wrote:
>The " Obviously you missed mine" post is accurate. I did. I don't get
where you are coming from. You can explain, but I don't care so it's not
really worth it, but I wouldn't mind a 2 line non-confrontational post

There is no way I'm going to take a non-confrontational approach with
anyone who sinks to the level of personal insults. Why would I do that?
I wouldn't bother taking any approach at all. Mabden is obviously a troll,
and clearly very hungry for attention.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 6 '06 #25
Mabden wrote:

But I think the whole "putting-me-in-your-killfile" would be a really
Good Thing.
That certainly sounds good to me. Done.

Brian
Oct 6 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by compiler | last post: by
geo039
reply views Thread by geo039 | last post: by
4 posts views Thread by Aaron | last post: by
19 posts views Thread by lost1 | last post: by
2 posts views Thread by J Ivan P Silvestre | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.