473,657 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1993
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******** *************** ***********@k36 g2000pri.google groups.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
5714
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
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.
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...
7
6168
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
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...
3
2764
by: swateedas | last post by:
please provide me the c code for pascal's triangle
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;
0
1131
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
8385
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
8303
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
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
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
8602
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
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
1941
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.