#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int create_matrix(int* matrix[], int row, int column, int size);
int create_pascals(int a, int b);

int main()
{
	//variable declaration
	int size;
	int* row = &size;
	int* column = &size;
	//output to the screen
	cout << " Please enter the size you want for your Pascal Triangle(# must be lesser than 10!!!): " << endl;
	cin >> size;
	//incase of number greater than 10 the program will terminate
	if(size>10)
	{
		cout<< " program will now terminate!!! thank you for using this program" << endl;
		exit(1);
	}
	//real stuff begins at this line
	else
	{
	//matrix declaration//
		int * *matrix= new int*[*row];
			for(int i=0; i<*row; i++)
			{
				matrix[i] = new int[*column];
			}
	//matrix declaration//

			//creating left blanks
	for(int row=0; row<size; row++)
	{
		for (int column=1; column<size-row; column++)
		{
			cout.width(2);
			cout << "D";
		}
		//introducing to fill the value
	for (int column=0; column<=row; column++)
	{
		cout.width(4);
		cout << create_pascals(row, column);
	}
	cout << endl;
	}

	//deletion
	for(int i=0; i<*row; i++)
		delete[] matrix[i];
	delete[]matrix;
	return 0;
	}
}
int create_pascals(int a, int b)
{
??????????????????????????????????????????????????????????????????????????????????
	int cons = 1, row;
	if (a< 0|| b<0|| b>a)
	{
		cout << " error!!! the program will now terminate." << endl;
		exit(1);
	}
????????????????????????????????????????????????????????????????????????????????
	else
	{
?????????????????????????????????????????????????????????????????????????????
	for (row=1; row<=b;row++, a--)
	{
		cons = cons*a/row;
	}
	return cons;
	}
????????????????????????????????????????????????????????????????????????????
}

i am trying to come up with code to make a pascal triangle using dynamic array this is what i have come up with so far. i need some explaination about some part of the code... thanks the code that i have question with is encircled with [???????], also can you tell me if my code is the right or wrong because i need it to be made with matrix(dymanic matrix) that why i declare it at a line.

thanks guys

Dani AI

Generated

create_pascals(a, b) is computing the binomial coefficient "a choose b" — the number that appears at row a and column b of Pascal's triangle. In the posted loop you call create_pascals(row, column), so a is the row index and b is the position inside that row. correctly pointed out the input checks; his description of the parameters as "length" and "height" is a bit misleading: they are indices (n and k), not counts in the abstract.

The function implements the standard multiplicative combination formula: conceptually
result = product_{i=1..b} (a - i + 1) / i.
The implementation does that by repeatedly multiplying by the next numerator and dividing by the step index so the intermediate values stay small. Mathematically each division is exact in this loop, but integer overflow is still possible for larger a. For safety use a wider integer type (for example long long) or limit a (your check size <= 10 is conservative).

Two practical issues in the posted program to fix:

  • The int* row = &size; int* column = &size; plus later loop variables named row/column cause shadowing and confusion. Remove those pointers and use plain loop variables.
  • You allocate a dynamic matrix but then print values from create_pascals without storing them. If the assignment requires a matrix, fill it (store each binomial into matrix[i][j]) and then print; delete rows first, then the pointer array.

If a safer, clearer approach is acceptable, build the triangle by recurrence (each entry is the sum of the two above). This both avoids division and fits naturally into a triangular container:

#include <vector>

std::vector<std::vector<long long>> pascal(n);
for (int i = 0; i < n; ++i) {
    pascal[i].assign(i + 1, 1);
    for (int j = 1; j < i; ++j)
        pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}

This produces a triangular matrix you can print (use std::setw from <iomanip> to align columns). : apply these fixes and the behavior of create_pascals will match the triangle values you expect.

Recommended Answers

All 5 Replies

int create_pascals(int a, int b)
{
/*
This section of the code is checking to make sure that the two parameters passed into the function are non-negative and that a is larger than b. Otherwise we exit straight away.
*/
	int cons = 1, row;
	if (a< 0|| b<0|| b>a)
	{
		cout << " error!!! the program will now terminate." << endl;
		exit(1);
	}

	else
	{
/*
This section of code is doing the calculation for what the method is calculating. By the looks of it, the second parameter passed into the function tells the method how many rows are in the pascal and the first parameter passed in tells it how long each row is.
*/
	for (row=1; row<=b;row++, a--)
	{
		cons = cons*a/row;
	}
	return cons;
	}

}

Hope this has helped,

darkagn

commented: good explanation. ithelp +2

but why do i want to pass the two parameters into the function? what does those two parameters do? ... thanks

The first parameter tells you how long the first row (I guess this is the base of your pascal triangle perhaps?) is. The second parameter tells you how many rows there are (the height of your pascal triangle?)

Maybe it would help to ask the person that actually wrote the code for you than having us try to figure out what he meant when he wrote it.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.