Check out my code on Pascal's triangle using C++.

#include "stdafx.h"
#include <iostream>
using namespace std;
int pascals(int,int);
int fact(int);
int main()
{
	int r,e,levels,space;
	cout<<"Enter the number of lines needed";cin>>levels;
	space = levels+1;
	for(r=0;r<=levels;r++)
	{
		//
		std::cout.width(space);//shape
		for(e=0;e<r+1;e++)
		{
			cout<<pascals(r,e)<<" ";	
		}
		cout<<endl;
		space--;//shape
	}
	cout<<endl;
	cin>>r;
	return 0;
	
}
int pascals(int r,int e)
{
	int temp;
	temp = fact(r) /(fact(e) * fact(r-e));
	return temp;
}
int fact(int num)
{
	int temp;
	if (num <=1)
		return 1;
	else
	{
		temp = num * fact(num -1);
	}
	return temp;
}

After removing the include for stdafx.h, it worked beautifully for me (for low number of lines) on Mac OS X. :)

Edited 6 Years Ago by dohpaz42: n/a

Check out my code on Pascal's triangle using C++.

#include "stdafx.h"
#include <iostream>
using namespace std;
int pascals(int,int);
int fact(int);
int main()
{
	int r,e,levels,space;
	cout<<"Enter the number of lines needed";cin>>levels;
	space = levels+1;
	for(r=0;r<=levels;r++)
	{
		//
		std::cout.width(space);//shape
		for(e=0;e<r+1;e++)
		{
			cout<<pascals(r,e)<<" ";	
		}
		cout<<endl;
		space--;//shape
	}
	cout<<endl;
	cin>>r;
	return 0;
	
}
int pascals(int r,int e)
{
	int temp;
	temp = fact(r) /(fact(e) * fact(r-e));
	return temp;
}
int fact(int num)
{
	int temp;
	if (num <=1)
		return 1;
	else
	{
		temp = num * fact(num -1);
	}
	return temp;
}

Using your code as a starting point, I've added support for larger numbers, error checking, and some formatting:

#include <iostream>
#include <iomanip>

using namespace std;

size_t pascals(size_t, size_t);
size_t fact(size_t);

int main()
{
    size_t e = 0, levels = 0, space = 0;

    do {
        cout << "Enter a number between 1 and 21, inclusive (Ctl-C to Abort): ";
        cin >> levels;
        cin.ignore();

        if (levels < 1 || levels > 21) {
            cout << ">> You must enter a number between 1 and 21, inclusive" << endl;
            levels = 0;
        }
    } while (levels == 0);

    space = levels + 1;

    for (size_t r = 0; r < levels; ++r) {
        cout << setw(space) << left << (r + 1) << " ";

        for (e = 0; e <= r; ++e) {
            size_t p = pascals(r, e);
            cout << p << " ";
        }

        cout << endl;

        space--; //shape
    }

    cout << endl;

    return 0;
}

size_t pascals(const size_t r, const size_t e)
{
    return fact(r) / (fact(e) * fact(r - e));
}

size_t fact(const size_t num)
{
    return num > 1 ? num * fact(num - 1) : 1;
}

Edited 6 Years Ago by dohpaz42: Fixed code typo

I think the reason why it seems to fall apart is the fact that 1 digit number occupies less space than 2 digit or three digit number. Maybe I need to look for a way to make all numbers occupy the same space then there will be no falling apart.

I think the reason why it seems to fall apart is the fact that 1 digit number occupies less space than 2 digit or three digit number. Maybe I need to look for a way to make all numbers occupy the same space then there will be no falling apart.

What do you mean by "1 digit number occupies less space than 2 digit...number."? I am new to C++, so I am still learning the ins-and-outs of keeping track of type sizes, and their sometimes subtle impact on a program. Thank you. :)

Sorry I did not mean the memory requirements, I meant the display space, 1, 111, 1111 the three numbers occupy different display space so affecting the smooth pattern.

However I also get what NathanOliver meant on falling apart not the pattern but the results...right...this is true but can e solved by declaring all variables and functions to type double. See the code...problem of pattern falling apart not sorted though...

#include <iostream>
using namespace std;
double pascals(double,double);
double fact(double);
int main()
{
	double r,e,levels;
	int space;
	cout<<"Enter the number of lines needed";cin>>levels;
	space = levels+1;
	for(r=0;r<=levels;r++)
	{
		//
		std::cout.width(space);//shape
		for(e=0;e<r+1;e++)
		{
			cout<<pascals(r,e)<<" ";	
		}
		cout<<endl;
		space--;//shape
	}
	cout<<endl;
	cin>>r;
	return 0;
	
}
double pascals(double r,double e)
{
	double temp;
	temp = fact(r) /(fact(e) * fact(r-e));
	return temp;
}
double fact(double num)
{
	double temp;
	if (num <=1)
		return 1;
	else
	{
		temp = num * fact(num -1);
	}
	return temp;
}

Sorry I did not mean the memory requirements, I meant the display space, 1, 111, 1111 the three numbers occupy different display space so affecting the smooth pattern.

Oh, ha. Yeah, that makes sense. I still forget that doubles can hold very large numbers. I see two things at this point:

1. The results should be stored in an array/container so that the max-length value can be found, and the spacing be determined before anything is sent to cout
2. Using a combination of the "fixed" floatfield flag and setprecision() to display very large numbers as very large numbers

I do not have time at this moment to try my own suggestions, but I will try them later. Thanks for the clarifications. This has been very insightful. :)

Edited 6 Years Ago by dohpaz42: n/a

This article has been dead for over six months. Start a new discussion instead.