VB icon

Triangular Pascals Triangle

Submitted on: 1/5/2015 6:50:00 PM
By: Josh Hollandsworth (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 381
     Prints out pascals triangle in a nice triangular format up to 13 rows


INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//INCLUDE files for :Triangular Pascals Triangle
iostream and iomanip
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Triangular Pascals Triangle
// Description:Prints out pascals triangle in a nice triangular format up to 13 rows
// By: Josh Hollandsworth (from psc cd)
// Inputs:You must input the number of rows of the triangle you wish to view.
// Side Effects:The program only handles ints and is limited to what an integer can do

//Author name: Josh Hollandsworth 
//Author email: j_hollandsworth@msn.com
//Source Filename: pascal.cpp
//Purpose: prints out a Pascalís triangle depending on the number of
// rows that a user inputs
//Date: 26/9/03 Last Modified 01/10/03 10:32
#include <iostream>
#include <iomanip>
using namespace std;
int fact(int num) {
//variable to hold the value of 
int num_factorial = 1;
	// while num is greater than one multiply it by the value that is referenced
// by num_factorial, then reassign it to num_factorial. Finally decrement the 
// value of num and start again
while(num > 1){
num_factorial *= num;
	}//end of while
	return num_factorial;
}// end of fact function
//comb finds the value of a combination of 2 numbers
int comb(int n , int k) {
	int combination = (fact(n)/(fact(k) * fact((n-k))));
	return combination;
}//end of comb function
//printTab prints out a specified number of rows determined by the max rows
//minus the number of the current row
void printTab(int n, int max){
//set the counter equal to max minus the current row
//then print out 4 spaces during ever iteration
for(int i =(max-n); i > 0; i--) {
		cout << "";
	}//end of for
}// end of printTab function
// main function
int main() {
	//vars to hold the variables in each loop
	int max,row,column;
	//print a blank line on execution to prevent the output from being right up against the CLI id
	cout << endl;
	//notes where the beginning of execution is for the goto loop
	// and prompts the user for input
	beginning: cout << "Enter the maximum number of rows: ";
	cin >> max;
	cout << endl;
	// if the maximum rows is above 14 go back so we prevent a buffer overflow of sourts
	if (max > 13 || max < 1){
	cout << "Number out of range...Try again!\n";
		goto beginning;
	}//end of goto if
	//increment row as long as it is less than the max rows;
	for(row = 0; row < max ; row++){
		//printTab aides in making output look better by tabbing over a n times depending on current
		//value of the row compared to the maximum number of rows
		cout << setw(2);
		printTab(row,(max - 1));
		//increment column as long as column is less then row +1.
		//this only works since the maximum values of numbers in a row is 1+the row number
		//ie in row 6 there are seven values in Pascalís triangle,.
		for(column = 0; column < row + 1; column++){
			// if the column number equals 0 output "1" since it would pass
			// 0 and 0 to the combination function which would cause execution fail
			// from a divide by zero exception
			if(row == 0){
			cout << setw(4);
				cout << "1";
			} else {
				//use the iomanip function setw to make the spacing between values to
				//ensure proper output and finally output the value and the spacing
				cout << setw(4);
				cout << comb(row,column) << "";
			}//end of row if else
		}//end of column for
		//output and end line to ensure the next row starts on a new line
		cout << endl;
	}//end of row for
	return 0;

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.