C++ exercises and solutions: Pascal triangle


C++ arrays: Pascal triangle

This is another C++ exercise about using two-dimensional array of C++. By using two-dimensional array, write C++ program to display a table that represents a Pascal triangle of any size. In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the example Pascal triangle(size=5) below:


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Solution:

 


#include<iostream>
#include<conio.h>
using namespace std;

void printPascalTr(int size);
int main()
    {

     int size;
     cout<<"Enter Pascal triangle size:";
     cin>>size;

     printPascalTr(size);
     getch();
     return 0;
 
    }
   


    void printPascalTr(int size){
         int PascalTr[size][size];
         int row,col;
         //assign zero to every array element
         for(row=0;row<size;row++)
            for(col=0;col<size;col++)  PascalTr[row][col]=0;
         //first and second rows are set to 1s  
         PascalTr[0][0]=1;
         PascalTr[1][0]=1;
         PascalTr[1][1]=1;
        
         for(row=2;row<size;row++){
              PascalTr[row][0]=1;
              for(col=1;col<=row;col++){
                        PascalTr[row][col]=PascalTr[row-1][col-1]+PascalTr[row-1][col];
                                        }
                            }
          //display the Pascal Triangle
          for(row=0;row<size;row++){
            for(col=0;col<=row;col++){               
                   printf("%d\t",PascalTr[row][col]);
                   }
            cout<<endl;  
            }
        
         }


Comments

Nico Tagle comment

 Nico Tagle

#include <iostream>

using namespace std;

int main() {

/*
*
* This is another C++ exercise about using two-dimensional array
* of C++. By using two-dimensional array, write C++ program to
* display a table that represents a Pascal triangle of any size.
* In Pascal triangle, the first and the second rows are set to 1.
* Each element of the triangle (from the third row downward) is
* the sum of the element directly above it and the element to
* the left of the element directly above it. See the example
* Pascal triangle(size=5) below:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
int dimension = -1;

do{

cout << "Please enter a dimension the triangle dimension > " << flush;
cin >> dimension;

}while(dimension < 1);


int pascal[dimension][dimension] = {0};

if(dimension > 0){

pascal[0][0] = 1;
pascal[1][0] = 1;
pascal[1][1] = 1;

}else
{
pascal[0][0] = 1;
}

for(int i = 2; i < dimension; i++){
for(int j = 0; j < dimension; j++){

if((j > 0) && (i >= j))
pascal[i][j] = pascal[i-1][j] + pascal[i-1][j-1];
else
pascal[i][j] = pascal[i-1][j];
}
}

for(int i = 0; i < (dimension); i++){
for(int j = 0; j < dimension; j++){
if(i>=j)
cout << pascal[i][j] << " " << flush;
}
cout << endl;
}





/* string animals[2][3]; //multidimensional array [row][columns]

string animals[2][3] = {
{ "fox", "dog", "cat" },
{ "mouse", "squirrel", "parrot" }
};

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << animals[i][j] << " " << flush;
}

cout << endl;

}

int numbers[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout << numbers[i][j] << " " << flush;
}

cout << "..." << endl;

}

*/

return 0;
}


2017-06-16
Alexander comment

 Alexander

The soltion is not executable, because it does not assign a constant value to the size variables of the array. If you want to have variable sizes for the triangle, you need to use a dynamic array.
My solution works with two 1D-arrays instead of a 2D-array.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int rows;
cout << "Rows of Pascal Triangle: ";
cin >> rows;
int* current_row = new int[rows];
int* next_row = new int[rows];
next_row[0] = 1;
current_row[0] = 1;
for (int i = 1; i < rows; i++) {
next_row[i] = 0;
current_row[i] = 0;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
next_row[j+1] = current_row[j] + current_row[j + 1];
}
for (int k = 0; k <= i; k++) {
cout << current_row[k] << " ";
}
for (int l = 1; l < rows; l++) {
current_row[l] = next_row[l];
}
cout << endl;
}
cout << endl;
delete[] next_row;
delete[] current_row;
return 0;
}


2016-12-25
sisay sebsibe comment

 sisay sebsibe

wow its so good codes


2016-04-05
sisay sebsibe comment

 sisay sebsibe

wow its so good codes


2016-04-05
Matheus comment

 Matheus

I am not being able to execute this code in visual studio 2010. It is prompting an error.


2015-04-23



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.