recursive pascal triangle

I was given an assignment to create a pascal triangle using only recursion but could not find any help online; so, I done it on my own. I finished and figured I would post my code on here for help cleaning it up and also to help anyone needing help with the same program.


#include <iostream>
#include <iomanip>

using namespace std;

typedef char ElementType;//so the user can change the data type

void triangle(ElementType start, ElementType finish, int rows);//prototype
void pascal(ElementType start, ElementType finish);//prototype

void triangle(ElementType start, ElementType finish, int rows)
{
if(start != finish)//check to make sure there is more than one piece of data
{
cout << setw(rows) << start;//left-hand justifies the triangle based on number of rows in the triangle
triangle(start+1,finish,rows*0);//starts recursion and eleminates spacing inbetween letters inside the triangle
cout << start;//prints the second half of the triangle(the reverse half)
}
else
{
cout << setw(rows) << finish;//base case where there is only one piece of data
}
}

void pascal(ElementType start, ElementType finish, int rows)
{
if((finish-1) >= (start-1))//checks to see when the finish element is equal to the start
//and also allows the recursive "stack" to store the start value
{
rows++;//bumps the number of rows based on the amount of data between and including start-finish
pascal(start,finish-1,rows);//starts the recursion process of eleminating the data down the one element
triangle(start,finish,rows);//prints out the data
cout << endl;//adds the next level to the triangle
}

}

int main()
{
cout << "Program Written by: Remy LaBeau" << endl;

pascal('a','m',0);
return 0;
}
Please use code tags
i had too done this program

int pascal(int,int);
void space(int,int);

int main()
{
    int number,i,j;
    printf("\nEnter the no. of rows required: ");
    scanf("%d",&number);
    for(i=1;i<=number;i++)
    {
        space(number-i,3);
        for(j=1;j<=i;j++)
        {
            printf("%3d",pascal(i,j));
            space(1,3);
        }
        printf("\n");
    }
    return 0;
}

int pascal(int row, int column)
{
    if(column == 0) // The 0th column element is assumed to 0
        return 0;
    else if(row == 1 && column == 1)
        return 1;
    else if(column > row) // assuming the element is zero (no of columns> no of rows)
        return 0;
    else
        return (pascal(row - 1, column - 1) + pascal(row - 1, column));
}

void space(int num, int mul) // for spaces in between elements
{
    int i;
    num *= mul;
    for(i=0; i<num; i++)
        printf(" ");
}
Topic archived. No new replies allowed.