I'm trying to write a program that prints out Euler's triangle as follows:
1
1
1,1
1,4,1
1,11,11,1
1,26,66,26,1
1,57,302,302,57,1
1,120,1191,2416,1191,120,1

It uses the following formula, where A is a vector of vectors: A[x][y] = (x-y)A[n-1][m-1]+(m+1)A[n-1][m]

I've already implemented the formula to the function I've created to printEulersTriangle(), but it doesn't seem to be generating the correct numbers and I'm also having trouble printing the first 2 rows which should have only one element, 1. My triangle prints as follows:
1
1,1
1,3,1
1,8,6,1
1,19,34,10,1
1,42,159,108,15,1
1,89,645,909,291,21,1
1,184,2380,6216,4182,708,28,1

Below is my function printEulersTriangle() and a working example of Pascals Triangle, The function call at the end is just a formatting function that prints it out as a triangle. Any tips on how to get the first 2rows correct or problems with my formula implementation are greatly appreciated!

void printEulersTriangle(int numRows)
{
vector< vector<int> > vect(numRows);
for (int n=0; n<vect.size(); n++)
{
vect[n] = vector<int>(n+1, 1);

for (int m = 1; m<vect[n].size()-1; m++)
vect[n][m] = ((n-m)*vect[n-1][m-1]) + ((m+1)*vect[n-1][m]);
}

prettyPrintVectorOfVectors(vect);   //prints a formatted triangle
}

Example of Pascals Triangle implementation for reference

void printPascalsTriangle(int numRows) {
vector< vector<int> > vect(numRows);
for (int i=0; i<vect.size(); i++)
{
vect[i] = vector<int>(i+1, 1);
for (int j = 1; j<vect[i].size()-1; j++)
vect[i][j] = vect[i-1][j-1] + vect[i-1][j];
}

prettyPrintVectorOfVectors(vect);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.