Pascal's triangle: Difference between revisions

Line 385:
<lang cpp>#include <iostream>
#include <algorithm>
#include <vectorcstdio>
using namespace std;
#include <iterator>
void genPyrNPascal_Triangle(int rowssize) {
 
int a[100][100];
void genPyrN(int rows) {
int i, j;
if (rows < 0) return;
// save the last row here
std::vector<int> last(1, 1);
std::cout << last[0] << std::endl;
for (int i = 1; i <= rows; i++) {
// work on the next row
std::vector<int> thisRow;
thisRow.reserve(i+1);
thisRow.push_back(last.front()); // beginning of row
std::transform(last.begin(), last.end()-1, last.begin()+1, std::back_inserter(thisRow), std::plus<int>()); // middle of row
thisRow.push_back(last.back()); // end of row
 
//first row and first coloumn has the same value=1
for (int j = 0; j <= i; j++)
for (int i = 1; i <= rowssize; i++) {
std::cout << thisRow[j] << " ";
a[i][1] = a[1][i] = 1;
std::cout << std::endl;
}
//Generate the full Triangle
for (i = 2; i <= size; i++) {
for (j = 2; j <= size - i; j++) {
if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
break;
}
a[i][j] = a[i - 1][j] + a[i][j - 1];
}
}
 
/*
1 1 1 1
1 2 3
1 3
1
first print as above format-->
for (i = 1; i < size; i++) {
for (int j = 01; j <= isize; j++) {
if (a[i][j] == 0) {
break;
}
printf("%8d",a[i][j]);
}
cout<<"\n\n";
}*/
// standard Pascal Triangle Format
int row,space;
for (i = 1; i < size; i++) {
space=row=i;
j=1;
while(space<=size+(size-i)+1){
cout<<" ";
space++;
}
while(j<=i){
if (a[row][j] == 0){
break;
}
if(j==1){
printf("%d",a[row--][j++]);
}
else
printf("%6d",a[row--][j++]);
}
cout<<"\n\n";
}
}
 
int main()
{
//freopen("out.txt","w",stdout);
int size;
cin>>size;
Pascal_Triangle(size);
}
 
last.swap(thisRow);
}
}</lang>