473,386 Members | 1,715 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,386 software developers and data experts.

Triangle of Pascal

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[n+1,n+1];
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1;k++)
{
if(k==0)
arr[i,k]=1;
else if(i==k)arr[i,k]=1;
}
}
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1+1;k++){
if(i>=1&&i+1<n+1&&k+1<n+1)
{
if(arr[i,k]!=0&&arr[i,k+1]!=0)
arr[i+1,k+1]=arr[i,k]+arr[i,k+1];
}
}
}
return arr;
}
Sep 8 '08 #1
2 1982
On Sep 8, 2:03*pm, J Ivan P Silvestre <ECDu...@gmail.comwrote:
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[n+1,n+1];
* * * * *for(int i=0;i<n+1;i++)
* * * * *{
* * * * * * for(int k=0;k<n+1;k++)
* * * * * * {
* * * * * * if(k==0)
* * * * * * arr[i,k]=1;
* * * * * * *else if(i==k)arr[i,k]=1;
* * * * * * *}
* * * * *}
* * * * *for(int i=0;i<n+1;i++)
* * * * *{
* * * * *for(int k=0;k<n+1+1;k++){
* * * * * * * *if(i>=1&&i+1<n+1&&k+1<n+1)
* * * * * * * *{
* * * * * * * * * if(arr[i,k]!=0&&arr[i,k+1]!=0)
* * * * * * * * * arr[i+1,k+1]=arr[i,k]+arr[i,k+1];
* * * * * * * *}
* * * * * * }
* * * * * *}
* * * * * * return arr;
* * * }
Thank for posting your code

Where is the question though?

Sep 8 '08 #2
I like this way:

public static int[,] TP2(int n)
{
int[,] arr = new int[n + 1, n + 1];
for (int r = 0; r < n + 1; ++r)
{
arr[r, 0] = 1;
arr[r, r] = 1;
if (r 0)
{
for (int c = 1; c < n; ++c)
arr[r, c] = arr[r - 1, c - 1] + arr[r - 1, c];
}
}
return arr;
}
"J Ivan P Silvestre" <EC*****@gmail.comwrote in message
news:b8**********************************@k36g2000 pri.googlegroups.com...
>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[n+1,n+1];
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1;k++)
{
if(k==0)
arr[i,k]=1;
else if(i==k)arr[i,k]=1;
}
}
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1+1;k++){
if(i>=1&&i+1<n+1&&k+1<n+1)
{
if(arr[i,k]!=0&&arr[i,k+1]!=0)
arr[i+1,k+1]=arr[i,k]+arr[i,k+1];
}
}
}
return arr;
}
Sep 9 '08 #3

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

Similar topics

25
by: GUPTAJI | last post by:
hi all, can u give me the code to create a Pascal's Triangle........... and yes, it should work thankx
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.
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...
7
by: harishram007 | last post by:
please can u help me out in the following patterns A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA PATTERN 2: 4 4 4 4
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...
3
by: swateedas | last post by:
please provide me the c code for pascal's triangle
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...
0
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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...

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.