In: Computer Science
In this question using c++, you have to design the Pascal triangle where you can create a list of coefficients of a binomial of any size that is passed to the constructor. Data members and member functions of this class are of your design choice. You need to use only pointers and pointers to pointers (do not use regular array notations). Create a two-dimensional array in the heap memory (which creates a dynamic memory). Use an application program to print out coefficients of a binomial in a triangle shape.
Output be like:
Enter the size of the Pascal triangle (to quit: -1): 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Enter the size of the Pascal triangle (to quit: -1): -1
Thank you for using my program. See you soon!
The solution code has been tried to keep as simple as possible to help you understand the code in a better way, Also the explanation of the code has been added with upper case letters as comment lines. Still if you couldn't understand anything or want to ask anything you can just mention in the comment section I will try to reach you as soon as possible. To allocate space dynamically here new keyword of c++ has been used, it could have been done by using malloc as well.
CODE:
#include<iostream>
using namespace std;
class PascalTriangle{ // CLASS DEFINED
public:
PascalTriangle(int _size){ // CONSTRUCTOR OF THE CLASS WHICH TAKES THE SIZE OF THE PASCAL TRAINGLE
n = _size; // INITIALIZED THE DATA MEMBER N WITH SIZE OF THE PASCAL TRAINGLE FROM CONSTRUCTOR
arr = new int*[n]; // ALLOCATED THE ARR POINTER TO N POINTERS DYNAMICALLY USING NEW KEYWORDS
for (int i=0;i<n;i++)
*(arr+i) = new int[n]; // SIMILARLY N INTEGERS ALLOCATED TO EACH THOSE POINTERS TO STORE THE COEFFICIENT OF PASCAL TRAINGLE, ALSO ARR[i] IS SAME AS *(ARR + i)
}
void calculateCoefficients(); // MEMBER FUNCTION DECLARATION WHICH WILL CALCULATE THE COEFFICIENTS AND STORE IN THE 2D ARRAY GENERATED
void printTriangle(); // MEMBER FUNCTION DECLARATION WHICH WILL PRINT THE PASCAL COEFFICIENTS IN THE SPECIFIED MANNER
private:
int n; // DATA MEMBERS OF THE CLASS, N WHICH DENOTES SIZE, AND A POINTER TO A POINTERS WHICH DENOTES 2D ARRAY
int **arr;
};
void PascalTriangle::calculateCoefficients(){ // MEMBER FUNCTION DEFINITION WHICH CALCULATES THE COEFFICIENTS
for (int l = 0; l < n; l++) { // L DENOTES ROWS, I.E, LINES
for (int i = 0; i <= l; i++) { // I DENOTES COLUMNS
if (l == i || i == 0) // IN A LINE LAST VALUE AND FIRST VALUE ARE 1
*(*(arr+l)+i) = 1;
else // AND OTHER VALUES ARE JUST ADDITION OF 2 VALUES FROM JUST UPPER ROWS(LINES) HAVING COLUMN VALUES COL-1 AND COL SO ADDED
*(*(arr+l)+i) = *(*(arr+l-1)+i-1) + *(*(arr+l-1)+i); // *(arr+l) denotes arr[l] and *(*(arr+l)+i) denotes arr[l][i]
}
}
}
void PascalTriangle::printTriangle(){ // MEMBER FUNCTION DEFINITION WHICH PRINTS THE COEFFICIENTS IN TRIANGE FORM
for (int l = 0; l < n; l++) { // FOR EVERY LINE
for (int i = 0; i <= l; i++) { // PRINT EACH VALUE OF THE ROW(LINE) SEPARATED BY SPACE
cout << *(*(arr+l)+i) << " ";
}
cout << "\n"; // CHANGE THE LINE AFTER EVERY ROW
}
}
int main() // MAIN FUNCTION
{
int n;
while (1){ // INFINITE LOOP TO KEEP ASKING THE USER SIZE OF THE PASCAL TRIANGLE UNTIL -1 IS ENTERED
cout<<"Enter the size of the Pascal triangle (to quit: -1): "; // ASK THE USER FOR SIZE
cin>>n;
if (n!=-1){ // IF SIZE ENTERED IS NOT -1 THEN
PascalTriangle p1(n); // MADE THE OBJECT OF PASCAL TRIANGLE CLASS PASSING THE SIZE IN ITS CONSTRUCTOR
p1.calculateCoefficients(); // CALLED THE CALCULATE FUNCTION FUNCTION
p1.printTriangle(); // AND THEN CALLED THE PRINT TRIANGLE FUNCTION
}
else // IF SIZE EQUALS -1 JUST BREAK THE INFINITE LOOP TO END THE PROGRAM
break;
}
return 0;
}
SCREENSHOT(SAMPLE INPUT + OUTPUT):
NOTE: If you have any doubt regarding the solution please ask in the comment section. if you want any modification or better code you can mention that there as well, HAPPY LEARNING