User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 375,169 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,294 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser:
Views: 4503 | Replies: 5
Reply
Join Date: Jan 2007
Posts: 1
Reputation: Maddy1987 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Maddy1987 Maddy1987 is offline Offline
Newbie Poster

Solution Pascal's Triangle

  #1  
Jan 12th, 2007





Coefficients in the expansion of (x+y)^n form a pattern which is useful for small values of n. It is known as Pascal's triangle.

n Pascal's triangle
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1

Write a Matlab script to calculate and display this expression in the form of the triangle for the first 10 values of n. Use the sprintf command to display in a suitable format such as that shown above.





% A Program for Pascal's Triangle

if nargin ~=2
n=1:10;
s=4;
end

if n<55
a= n/10;
x=0;
y=0;
x2=0;
w=1;


hold on;

for m = 1:n-1
x = x2-a/2;
y = y-a/2*sqrt(2);
w = 1;

x2 = x;

for l = 1:m
x = x+a;
w = nchoosek(m,1);

end
end

else clf;
text(-0.2,0.5,' FontSize',23);
end;

hold off;
axis square;
axis equal;
axis off;


I had tried the question but i am not getting through it can any1 help me out in this problem.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,596
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 294
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Pascal's Triangle

  #2  
Jan 14th, 2007
Try to plan out a flow chart first.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Apr 2008
Posts: 1
Reputation: saurabh41_scs is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
saurabh41_scs saurabh41_scs is offline Offline
Newbie Poster

Re: Pascal's Triangle

  #3  
Apr 13th, 2008
Originally Posted by Maddy1987 View Post



Coefficients in the expansion of (x+y)^n form a pattern which is useful for small values of n. It is known as Pascal's triangle.

n Pascal's triangle
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1

Write a Matlab script to calculate and display this expression in the form of the triangle for the first 10 values of n. Use the sprintf command to display in a suitable format such as that shown above.





% A Program for Pascal's Triangle

if nargin ~=2
n=1:10;
s=4;
end

if n<55
a= n/10;
x=0;
y=0;
x2=0;
w=1;


hold on;

for m = 1:n-1
x = x2-a/2;
y = y-a/2*sqrt(2);
w = 1;

x2 = x;

for l = 1:m
x = x+a;
w = nchoosek(m,1);

end
end

else clf;
text(-0.2,0.5,' FontSize',23);
end;

hold off;
axis square;
axis equal;
axis off;


I had tried the question but i am not getting through it can any1 help me out in this problem.

hello i want to write code for this
in my opinion pascal triangle is:-
1
1 1
1 2 1
1 3 3 1

u wil find this by following prog:-
/*Program to Calculate the Pascal triangle */
#include<stdio.h>
int main()
{
int a[20][20];
int i,j,c,n,k;
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
a[2][1]=1;a[2][2]=1;
for(i=1;i<=n;i++)
{ for(j=1;j<=n-i;j++)
printf(" ");
if(i<3)
{
for(k=1;k<=i;k++)
{
printf("%d ",a[i][k]);
}
printf("\n");
}
else
{
a[i][1]=1;
printf("%d ",a[i][1]);
j=2;c=3;
while(j<i)
{
a[i][j]=a[i-1][c-2]+a[i-1][c-1];
printf("%d ",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d\n ",a[i][j]);
}
}
getche();
}
Reply With Quote  
Join Date: Feb 2008
Posts: 8
Reputation: DevonMcC++ is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Pascal's Triangle

  #4  
Apr 28th, 2008
These seem rather elaborate in comparison to something like (in J):
Pascal =: i. !/ i.

See http://www.jsoftware.com/jwiki/NYCJUG/Projects/Pascal or http://www.jsoftware.com/jwiki/Essays/Pascal's_Triangle for more.
Reply With Quote  
Join Date: Oct 2007
Location: Cambridge, MA
Posts: 199
Reputation: sarehu is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sarehu's Avatar
sarehu sarehu is online now Online
Junior Poster

Re: Pascal's Triangle

  #5  
May 5th, 2008
That's because J is deliberately cryptic and hides all the hard work behind pre-written combinators and functions, in this case ! being "choose". Given the equivalent of these two defined in Haskell:

comb n k = foldl' (\x y -> ((n - x + 1) * y) `div` x) 1 [1..k]

table f xs ys = foldr ((:) . (`map` ys) . f) [] xs

Then producing Pascal's triangle is as simple as join (table comb) [0..] .

But overall a much simpler solution is
iterate (\xs -> zipWith (+) (0:xs) (xs++[0])) [1]
Reply With Quote  
Join Date: Feb 2008
Posts: 8
Reputation: DevonMcC++ is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Pascal's Triangle

  #6  
May 9th, 2008
I suppose anything is cryptic if you make no effort to understand it.

Even so, your statement is very odd. Apparently you are under the mis-apprehension that your failure to understand is the fault of a succinct, powerful notation and this means that the language was "deliberately" designed to be hard to understand.

Why would you even think something like this? I mean, why would someone deliberately make something difficult to understand? Most people I know strive for clarity. Do you think that "1+1" is cryptic because it uses a symbol "+" instead of the word "plus"? I suppose it is if you don't know math.

The other odd thing you seem to imply is that avoiding hard, unnecessary work is a bad thing. So, you're against the idea of carefully designed, well-tested, powerful, pre-written functions that you can combine in a clear, logically consistent manner?

This is all the more puzzling as your initial answer
join (table comb) [0..] apparently very closely resembles the J solution of
i. !/ i. because each "i." generates a vector of consecutive integers and "/" - similarly to (table comb) - combines these vectors in a table using "!". The "!" function, with a right and left arguments x and y returns the number of ways x things can be chosen from y possibilities - pretty much a direct statement of what you are doing when determining the coefficient of a polynomial expansion.

There are, of course other ways to do this more algorithmically and less mathematically as you appear to show in your final example. I say "appear" because that example is rather cryptic insofar as it uses the pre-written function "zipWith".

In J, I could write something I presume is similar to this:
pascalNewRow=: 3 : '1,(2+/\y),1' based on the following rule for generating a new row of the triangle from the previous one: start and end with 1; add together adjacent pairs for the middle section. The above expression "2+/\" adds together each adjacent 2 numbers across the first dimension of an array; the "3 :" defines a function. Once you know that, the code is a straightforward expression of the rule.

Similarly to the final expression in Haskell, we iterate the function "pascalNewRow" by using an iterator expressed by some symbols, "^:" instead of the word "iterate". So, the expression
pascalNewRow^:10 ] 1 returns the first ten rows of Pascal's triangle (starting with a "seed" of 1).

I find this much less cryptic than the Haskell expression but, of course, I've made the effort to understand it.

In any case, this has wandered far off topic. Why don't I conclude by giving a solution to the original problem? Taking my J code as a template, start by defining a function to add adjacent pairs of a vector:
int *addAdjacentPairs( int vlen, int *vec)
{   int vc, win2[2];
    win2[0]=0;
    win2[1]=vec[0];
    for(vc=0;vc<=vlen;vc++)
    {   vec[vc]=win2[0]+win2[1];
        win2[0]=win2[1];
        win2[1]=vec[vc+1];
    }
// Danger: returned vector is 1 longer than input: allocate accordingly.
    return 0;
}
and simply use it in a loop:
// PascalTriangle.c: print some rows of Pascal's triangle.

#include<stdio.h>

int main(void)
{   int vec[21];
    int ii,vc,nn;
    printf("How many lines (up to 20) do you want? ");
    scanf("%d",&nn);
// Should verify result and handle errors but this is why I don't like these
// languages that don't handle arrays natively so I won't bother.
    printf("\n");

    for(ii=0;ii<nn;ii++)
    {   vec[ii]=1;
        for(vc=0;vc<=ii;vc++) printf("%i ",vec[vc]);
        printf("\n");
        addAdjacentPairs( ii, vec);
     }
    return 0;
}

This provides a nice example of how solving the problem with a high-level notation leads to a more elegant solution even in a lower-level language.

Originally Posted by sarehu View Post
That's because J is deliberately cryptic and hides all the hard work behind pre-written combinators and functions, in this case ! being "choose". Given the equivalent of these two defined in Haskell:

comb n k = foldl' (\x y -> ((n - x + 1) * y) `div` x) 1 [1..k]

table f xs ys = foldr ((:) . (`map` ys) . f) [] xs

Then producing Pascal's triangle is as simple as join (table comb) [0..] .

But overall a much simpler solution is
iterate (\xs -> zipWith (+) (0:xs) (xs++[0])) [1]
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Legacy and Other Languages Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Legacy and Other Languages Forum

All times are GMT -4. The time now is 11:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC