program:
#include<iostream.h>
#include<conio.h>
void main()
{
int p,q,l,f,m,s,t,v,n=6;
int a[][]=new int[100][100];
l=(2*n)-1;
f=n-1;
a[0][f]=1;
a[n-1][0]=1;
a[n-1][l-1]=1;
for(p=0;p<n;p++)
{
int a[][]=new int[100][100];
l=(2*n)-1;
f=n-1;
a[0][f]=1;
a[n-1][0]=1;
a[n-1][l-1]=1;
for(p=0;p<n;p++)
{
for(q=2;q<l;q++)
{
a[p+1][q-1]=a[p][q]+a[p][q-2];
}
}
for(p=0;p<n;p++)
{
a[p+1][q-1]=a[p][q]+a[p][q-2];
}
}
for(p=0;p<n;p++)
{
for(q=0;q<l;q++)
{
if(a[p][q]==0) {cout<<" "; }
{
if(a[p][q]==0) {cout<<" "; }
else { cout<<a[p][q]; }
}
cout<<"\n";
}
getch();
}
======================================
step1:
Considering basic pascal model
1
1 1
let take it as a 2-D array "a" then we have
a[0][0]=nothing or 0
a[0][1]=1
a[0][2]=nothing or 0
a[1][0]=1
a[1][1]=nothing or 0
a[1][2]=1
so for given number of rows say n(here it is 2) we have
index of first row 0 and last row n-1(here it is 1)
index of first column 0 and last column 2n-1(here it is 3)
let l=2n-1
it is 2*3 matrix now.
now onwards you should concider a matrix of 2*3
step2:
in all pascal models it is necessary to have
* middle element of first row should be 1
so we have (2n-1) columns find the middle of it it will be n-1.
let f=n-1
a[0][f]=1;
* first element of last row must be 1
row index of first element of last row n-1
column index of first element of last row 0
so a[n-1][0]=1
* last element of last row must be 1
row index of last element of last row n-1
column index of last element of last row l=2n-1
step3:
now we have matrix like this
0 1 0
1 0 1
in order to understand the concept i will take higher pascal model mean while
consider
row0--> 0 0 0 0 1 0 0 0 0
row1--> 0 0 0 1 0 1 0 0 0
row2--> 0 0 1 0 2 0 1 0 0
row3--> 0 1 0 3 0 3 0 1 0
row4--> 1 0 4 0 6 0 4 0 1
in this i will tell you how a row is obtained from it's previous row
row 3 is obtained from row 2:
0th element of 3rd row= -1th element of 2nd row+ 1st element of 2nd row
= 0+0
= 0
1st element of 3rd row= 0th element of 2nd row+ 2nd element of 2nd row
= 0+1
= 1
2nd element of 3rd row= 1st element of 2nd row+ 3rd element of 2nd row
= 0+0
= 0
3rd element of 3rd row= 2nd element of 2nd row+ 4th element of 2nd row
= 1+2
= 3
and so on..
lets apply general rule :
(q-1) th element of (p+1) row= qth element of pth row+ (q-2)th element of pth row
a[0][1]
a[p][q]
^
0 1 0
1 0 1
^ ^
a[p+1][q-1] a[p+1][q+1]
a[1][0] a[1][1]
so a[p+1][q-1]=a[p][q]+a[p][q-2];
Step4:
replace all 0s with spaces and print the matrix....
Considering basic pascal model
1
1 1
let take it as a 2-D array "a" then we have
a[0][0]=nothing or 0
a[0][1]=1
a[0][2]=nothing or 0
a[1][0]=1
a[1][1]=nothing or 0
a[1][2]=1
so for given number of rows say n(here it is 2) we have
index of first row 0 and last row n-1(here it is 1)
index of first column 0 and last column 2n-1(here it is 3)
let l=2n-1
it is 2*3 matrix now.
now onwards you should concider a matrix of 2*3
step2:
in all pascal models it is necessary to have
* middle element of first row should be 1
so we have (2n-1) columns find the middle of it it will be n-1.
let f=n-1
a[0][f]=1;
* first element of last row must be 1
row index of first element of last row n-1
column index of first element of last row 0
so a[n-1][0]=1
* last element of last row must be 1
row index of last element of last row n-1
column index of last element of last row l=2n-1
step3:
now we have matrix like this
0 1 0
1 0 1
in order to understand the concept i will take higher pascal model mean while
consider
row0--> 0 0 0 0 1 0 0 0 0
row1--> 0 0 0 1 0 1 0 0 0
row2--> 0 0 1 0 2 0 1 0 0
row3--> 0 1 0 3 0 3 0 1 0
row4--> 1 0 4 0 6 0 4 0 1
in this i will tell you how a row is obtained from it's previous row
row 3 is obtained from row 2:
0th element of 3rd row= -1th element of 2nd row+ 1st element of 2nd row
= 0+0
= 0
1st element of 3rd row= 0th element of 2nd row+ 2nd element of 2nd row
= 0+1
= 1
2nd element of 3rd row= 1st element of 2nd row+ 3rd element of 2nd row
= 0+0
= 0
3rd element of 3rd row= 2nd element of 2nd row+ 4th element of 2nd row
= 1+2
= 3
and so on..
lets apply general rule :
(q-1) th element of (p+1) row= qth element of pth row+ (q-2)th element of pth row
a[0][1]
a[p][q]
^
0 1 0
1 0 1
^ ^
a[p+1][q-1] a[p+1][q+1]
a[1][0] a[1][1]
so a[p+1][q-1]=a[p][q]+a[p][q-2];
Step4:
replace all 0s with spaces and print the matrix....
No comments:
Post a Comment