Implement a C function, named triangle, that outputs Pascal’s triangle. In general, the Pascal’s triangle can be represented as: 0C0 1C1 1C0 2C2 2C1 2C0 3C3 3C2 3C1 3C0 4C4 4C3 4C2 4C1 4C0 . . . where nCr represents how many ways there are to choose r from n, not counting duplicates. The formula used to calculate nCr can be written as: nCr = n!/r!(n −r)! where n! is the factorial of n. The function triangle: • is called by value where exactly one parameter (the number of rows of Pascal’s triangle) is passed to it. • returns void (i.e., no value). • prints out Pascal’s triangle to the standard output. • Employs two other functions, namely: – int choose(int n, int r); That chooses r from n – int factorial(int n); That calculates the factorial of n. Note: You are allowed — in fact, encouraged — to design and use any other functions as needed. Note: • The number of the rows is limited to integers between 0 and 13 inclusive. • If 0 is entered by the user, no triangle will be printed. • If a negative integer is entered by the user, the program terminates. • The number displayed in the last column must always be displayed at the start of the line. • Carefully calculate the spaces used between the numbers so that the numbers are properly aligned across rows.
Implement a C function, named triangle, that outputs Pascal’s triangle.
In general, the Pascal’s triangle can be represented as:
0C0
1C1 1C0
2C2 2C1 2C0
3C3 3C2 3C1 3C0
4C4 4C3 4C2 4C1 4C0
. . .
where nCr represents how many ways there are to choose r from n, not counting duplicates.
The formula used to calculate nCr can be written as:
nCr = n!/r!(n −r)!
where n! is the factorial of n.
The function triangle:
• is called by value where exactly one parameter (the number of rows of Pascal’s triangle)
is passed to it.
• returns void (i.e., no value).
• prints out Pascal’s triangle to the standard output.
• Employs two other functions, namely:
– int choose(int n, int r);
That chooses r from n
– int factorial(int n);
That calculates the factorial of n.
Note: You are allowed — in fact, encouraged — to design and use any other functions as needed.
Note:
• The number of the rows is limited to integers between 0 and 13 inclusive.
• If 0 is entered by the user, no triangle will be printed.
• If a negative integer is entered by the user, the program terminates.
• The number displayed in the last column must always be displayed at the start of the line.
• Carefully calculate the spaces used between the numbers so that the numbers are properly
aligned across rows.
Step by step
Solved in 2 steps