473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 5685
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Statsstudent2006 | last post by:
Hello! I am a newbie, and have received my first VBA assignment. I have to write a subroutine that generates the Fibonacci sequence of numbers (the first n numbers, where n is an integer number...
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
0
by: compiler | last post by:
hai, iam new to c,i need c-code for pascal triangle,could you please provide me the code. thanks and regards.
0
geo039
by: geo039 | last post by:
I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 constructors...
4
by: Aaron | last post by:
Hi, I have written a pascals triangle program. However, my program gets a floating point exception somewhere between 60 and 70 and any subsequent larger value. This is no doubt due to dividing...
19
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
3
by: abraxas91 | last post by:
Hi, my name is Stephan. I've been working on this program off and on over the past few days. I have yet to perfect it. I would really appreciate any help! For some reason the output begins to...
2
by: J Ivan P Silvestre | last post by:
c #: Implementing the method Int TrianglePascal (int n) which returns the triangle to the level of Pascal N. public static int TrianglePascal(int n) { int arr = new int; for(int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.