CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2025-11-07
by Andy Richter

Original Post

Original - Posted on 2025-11-03
by Andy Richter



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I used strings. First we find how long the last row will be. Then we can use it to center all the other rows. I also used a one row at a time function to find each column value. Your recurse function
``` #include <iostream> #include <string> using namespace std;

// Forward declaration of a function. int Pascal (int row, int column);

/* The main function. * * Parameters: * none * * Return value: * 0 if we complete successfully, 1 if there was an error. */
int main () {
// introduction
cout << "\nPascal's Triangle!\n"; cout << "(Pascal's triangle is made by taking the sum of two numbers\n"; cout << "and placing that number directly underneath the two numbers.\n"; cout << "This creates a triangular array of binomial coefficients)\n\n";

// for loops to calculate and print out pascal's triangle string lastRow,aRow; for (int column = 0; column <= 15; column++) { lastRow+=to_string(Pascal(15,column))+" "; } int Center = lastRow.size(); // longest, use for centering for (int row = 0; row < 15; row++) { aRow=""; // new row
for (int column = 0; column <= row; column++) { aRow+=to_string(Pascal(row,column))+" "; } cout << std::string((Center-aRow.length())/2,' '); cout << aRow << endl; } cout << lastRow << endl; cout << endl; }
int Pascal (int row, int column) {
// if statements to calculate pascal's triangle through recursion
if (column == 0) return 1;
else if (row == column) return 1;
else return Pascal(row - 1, column - 1) + Pascal(row - 1, column); } ``` Output: ``` $ g++ -o test stk_pascal.cpp $ ./test
Pascal's Triangle! (Pascal's triangle is made by taking the sum of two numbers and placing that number directly underneath the two numbers. This creates a triangular array of binomial coefficients)
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

```
Your code is broken because every Pascal(r,n) call sets r = 0 just before the call. All results are 1.
I used a nCr() to calculate each column for each row. I used strings so I could easily indent each row. Get largest row first and use it's length to indent the smaller rows. Then print the last row string. By using nCr I avoided seg faults for huge factorials. I avoided recursion for the same reason...too large = too long. 21! equals (51,090,942,171,709,440,000), which exceeds the maximum value an `unsigned long long` can store, leading to an overflow.
``` #include <iostream> #define DEFAULT_TRI 12
/* Calc one column of a row of Pascal's Triangle */ std::string nCr(int row, int col) { if (col < 0 || col > row) return "Invalid"; // Invalid column for row // Edges of Pascal's triangle are always 1 if (col == 0 || col == row) return std::to_string(1); unsigned long long res = 1; // Edge value always 1 // Optimize by choosing the smaller k for calculation: C(n, k) = C(n, n-k) if (col > row / 2) col = row - col; for (int i = 0; i < col; ++i) res = res * (row - i) / (i + 1); return std::to_string(res); }
/* Print rows size Pascal's Triangle */ void pascal(int rows) { std::string aRow,maxRow; int M; for (int i=0;i<=rows;i++) // longest row maxRow += nCr(rows,i) + " "; M = maxRow.length(); for (int r=0;r<rows;r++) { // print all but maxRow aRow = ""; // new row for (int i=0;i<=r;i++) aRow += nCr(r,i) + " "; // each number std::cout << std::string((M-aRow.length())/2,' ') << aRow << std::endl; } std::cout<< maxRow<< std::endl; return; } int main(int argc, char** argv) { int rows = (argc > 1) ? atoi(argv[1]) : DEFAULT_TRI; if (rows > 63) rows = 63; // avoids unsigned long long overflow pascal(rows); return 0; } ```
Output:
``` $ g++ -o test pascal.cpp $ ./test 15 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 ```
Row 63 looks like:
``` 1 63 1953 39711 595665 7028847 67945521 553270671 3872894697 23667689815 127805525001 615790256823 2668424446233 10468434365991 37387265592825 122131734269895 366395202809685 1012974972473835 2588713818544245 6131164307078475 13488561475572645 27619435402363035 52728013040874885 93993414551124795 156655690918541325 244382877832924467 357174975294274221 489462003181042451 629308289804197437 123415381704736506 139870765932034706 148894686314746622 148894686314746622 139870765932034706 123415381704736506 629308289804197437 489462003181042451 357174975294274221 244382877832924467 156655690918541325 93993414551124795 52728013040874885 27619435402363035 13488561475572645 6131164307078475 2588713818544245 1012974972473835 366395202809685 122131734269895 37387265592825 10468434365991 2668424446233 615790256823 127805525001 23667689815 3872894697 553270671 67945521 7028847 595665 39711 1953 63 1 ``` Here is one that gets the same output using a 2d table: ``` #include <iostream> #include <vector> #define ull unsigned long long
std::vector<std::vector<ull>> generatePascalsTriangle(int numRows) { std::vector<std::vector<ull>> triangle; if (numRows < 0) return triangle; // invalid triangle numRows+=1; // triangle of size numRows triangle.resize(numRows);
for (int row = 0; row < numRows; ++row) { // Each row 'row + 1' elements triangle[row].resize(row + 1); // The first and last elements of each row are always 1 triangle[row][0] = 1; // like [1,2,1] or [1,5,10,10,5,1] triangle[row][row] = 1; // Calculate the middle elements for (int col = 1; col < row; ++col) { triangle[row][col] = triangle[row-1][col-1] + triangle[row-1][col]; } } return triangle; }
int main() { int rows,M; std::string lastRow,aRow; std::cout << "Enter the number of rows for Pascal's Triangle: "; std::cin >> rows; if (rows > 63) rows = 63; // avoid unsigned long long overflow std::vector<std::vector<ull>> pascalTriangle = generatePascalsTriangle(rows); for (int col = 0; col < pascalTriangle.size(); ++col) { lastRow += std::to_string(pascalTriangle[rows][col]) + " "; // get largest row } M = lastRow.length(); // used for spacing // Print the triangle rows for (int row = 0; row < pascalTriangle.size()-1; ++row) { aRow = ""; for (int col = 0; col < pascalTriangle[row].size(); ++col) { aRow += std::to_string(pascalTriangle[row][col]) + " "; // build a Row } // Add leading spaces for formatting std::cout << std::string((M-aRow.length())/2,' '); std::cout << aRow << std::endl; // append the row } std::cout << lastRow << std::endl; // print last row we got earlier return 0; } ``` Here is one with GMP. It does not have overflow at 63 rows. The results are up to your patience. compile with `g++ -o test pascal_GMP.cpp -lgmp -lgmpxx `
``` #include <iostream> #include <gmp.h> #include <gmpxx.h> // Includes C++ wrappers for GMP #define DEFAULT_TRI 12
/* Calc one column of a row of Pascal's Triangle */ std::string nCr(int row, int col) { if (col < 0 || col > row) return "Invalid"; // Invalid column for row // Edges of Pascal's triangle are always 1 if (col == 0 || col == row) return std::to_string(1); mpz_class res = 1; // Edge value always 1 // Optimize by choosing the smaller k for calculation: C(n, k) = C(n, n-k) if (col > row / 2) col = row - col; for (int i = 0; i < col; ++i) res = res * (row - i) / (i + 1); return res.get_str(); }
/* Print rows size Pascal's Triangle */ void pascal(int rows) { std::string aRow,maxRow; int M; for (int i=0;i<=rows;i++) // longest row maxRow += nCr(rows,i) + " "; M = maxRow.length(); for (int r=0;r<rows;r++) { // print all but maxRow aRow = ""; // new row for (int i=0;i<=r;i++) aRow += nCr(r,i) + " "; // each number std::cout << std::string((M-aRow.length())/2,' ') << aRow << std::endl; } std::cout<< maxRow<< std::endl; return; } int main(int argc, char** argv) { int rows = (argc > 1) ? atoi(argv[1]) : DEFAULT_TRI; pascal(rows); return 0; } ``` Here is the end of row 200: ``` 28586897941831487833832229719806133874800 6449483072242469816169466461175774105900 1407159943034720687164247227892896168560 296690349435031470185232849254526300600 60404023238269880157472556135652061200 11865075993231583602360680669503083450 2246641608185861983878945452213601600 409681705022127773530866523638950880 71873983337215398865064302392798400 12118287888251433529574795170878800 1961341392318151091491874362916800 304346078118333790059083952866400 45217131606152448808778187283008 6422888012237563751246901602700 870900069455940847626698522400 112532031446554154468618348400 13830752468291572057595551200 1613587787967350073386147640 178296993145563544020568800 18613422361350040309839600 1830828428985249866541600 169152626591028520278300 14629416353818682834880 1179791641436990551200 88326646952501966400 6107693672247476400 387790074428411200 22451004309013280 1175445251780800 55098996177225 2283896214600 82408626300 2535650040 64684950 1313400 19900 200 1 ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;