473,654 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5713
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(unsig ned 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(unsig ned long *row){
unsigned long v;
while( 0UL != (v = *row++)){
printf("%lu ", v);
}
printf("\n");
}

int card_row(unsign ed long *row){
int i;
i=0;
while( 0UL != *row++){
i++;
}
return i;
}

unsigned long * next_row(unsign ed 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_c ur);

for( c= 0; c< 20; c++){

row_next = next_row(row_cu r);
print_row(row_n ext);
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.eduwrot e in message
news:Hj1Vg.755$ rS.421@fed1read 05...
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(unsig ned 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1419
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 that the user inputs). For example, the first seven numbers will be 0,1,1,2,3,5,8). I also have to write a subroutine that generates Pascal's triangle (the first n rows, where n is an integer number that the user inputs). For example, the first...
5
6647
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 and then tell what kind of triangle the 3 lengths form. Function main should consist mainly of a loop and calls to functions. Let the functions do the work. (Think of main as the boss who delegates all of the work to others.) main: loops until...
0
2052
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
1841
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 (get/set) to take the lengths. Now I need two readonly (get only) constructors to determine whether something is right or equilateral. I'm really not sure how I would write the formula in vb. I understand a right triangle is a2 + b2 = c2 but i'm not...
4
6669
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 two large numbers in the nCr function. By using uint64_t instead of int helped get it this far. I would like to see if it is possible to use it for larger values. Unlike with most programs, execution time is of absolutely no concern. Does...
19
8255
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 class executes the code you see below. What I'm trying to figure out is what I'm doing wrong in getting the triangle type to print. the integers typed in by the user print fine, but I keep getting "null" for the triangle type because I don't know how...
6
13353
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 triangle that aligns to the left of the screen, but it loops and disappears immediately. So, my problem is getting it to stay on the screen and then ask the user if they want to create another one AND to make it print the first symbol in the center of...
3
2345
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 deteriorate at around row 13...I can't figure out why. Thank you in advance, Stephan import java.io.*; import java.util.*; import gpdraw.*; import java.lang.*; public class pascal{ int array= new int;
2
1993
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 i=0;i<n+1;i++)
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.