Pascal's triangle: Difference between revisions

restored BASIC and C implementations
m (final fix for problems due to the adding of apl)
(restored BASIC and C implementations)
Line 102:
1 4 6 4 1
1 5 10 10 5 1</lang>
 
=={{header|BASIC}}==
===Summing from Previous Rows===
{{works with|FreeBASIC}}
This implementation uses an array to store one row of the triangle.
DIM initializes the array values to zero. For first row, "1" is then stored in the array.
To calculate values for next row, the value in cell (i-1) is added to each cell (i).
This summing is done from right to left so that it can be done on-place, without using a tmp buffer.
Because of symmetry, the values can be displayed from left to right.
 
Space for max 5 digit numbers is reserved when formatting the display.
The maximum size of triangle is 100 rows, but in practice it is limited by screen space.
If the user enters value less than 1, the first row is still always displayed.
 
<lang freebasic>DIM i AS Integer
DIM row AS Integer
DIM nrows AS Integer
DIM values(100) AS Integer
 
INPUT "Number of rows: "; nrows
values(1) = 1
PRINT TAB((nrows)*3);" 1"
FOR row = 2 TO nrows
PRINT TAB((nrows-row)*3+1);
FOR i = row TO 1 STEP -1
values(i) = values(i) + values(i-1)
PRINT USING "##### "; values(i);
NEXT i
PRINT
NEXT row</lang>
 
=={{header|C}}==
 
{{trans|Fortran}}
 
<lang c>#include <stdio.h>
 
void pascaltriangle(unsigned int n)
{
unsigned int c, i, j, k;
 
for(i=0; i < n; i++) {
c = 1;
for(j=1; j <= 2*(n-1-i); j++) printf(" ");
for(k=0; k <= i; k++) {
printf("%3d ", c);
c = c * (i-k)/(k+1);
}
printf("\n");
}
}
 
int main()
{
pascaltriangle(8);
return 0;
}</lang>
 
 
6,962

edits