Triangulo de Pascal

 /* pascal.c: print rows of Pascal's triangle to stdout. The algorithm
is simply to generate successive rows iteratively using the
defining property of pascal's triangle:
 
C(n,k) = C(n-1,k-1) + C(n-1,k).
 
The program is written for simplicity rather than efficiency.
 
The only tricky thing is getting the display to look right.
We provide an option -c to compute the number of display
columns needed without doing the display itself.
 
Author: Terry R. McConnell
 
Compile: cc -o pascal pascal.c
*/
 
#include<stdio.h>
#include<stdlib.h>
 
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
 
#define PROGRAM_NAME "pascal"
#define VERSION "1.0"
#define MAX_ROWS 30
#define USAGE "usage: pascal [ -h -v] [-c rows] rowsn"
#define HELP "nnpascal [-h -v] [-c n] n n
print the first n rows of pascal's triangle to stdout.n
-h: print this helpful informationn
-v: print version number and exitn
-c: print the number of columns required for display only nn"
 
static int triangle[MAX_ROWS + 1][MAX_ROWS];
 
/* num_digits: count how man digits the argument has and return that number */
 
int num_digits(int n) {
 
int d = 1;
 
 
while(n=n/10)d++;
return d;
}
 
/* print_centered: print the first argument centered in a field of
width given by the second argument, to the extent possible.
(The width is always the second argument, but the number may not
be quite centered, depending on parity.)
Return 0 if successful and 1 if not.
*/
 
int print_centered(int n, int width)
{
int nw;
int pad_space;
int i;
 
nw = num_digits(n);
if(width < nw) return 1; /* no can do */
 
pad_space = (width - nw)/2;
 
for(i=0;i<pad_space;i++)printf(" ");
printf("%d",n);
for(i=0;i<width - nw - pad_space;i++)printf(" ");
return 0;
}
 
int main(int argc, char **argv){
 
int i=1,j;
int cols_option = FALSE;
int nrows;
int max_width;
int row_offset;
 
/* Process command line */
 
while(((i+1)<=argc)&&(argv[i][0] == '-')){
if(argv[i][1] == 'h'){
printf(HELP);
return 0;
}
if(argv[i][1] == 'v'){
printf("%sn",VERSION);
return 0;
}
if(argv[i][1]=='c'){
/* make sure there is an arg */
if((i+1) >= argc){
fprintf(stderr,USAGE);
return 1;
}
cols_option = TRUE;
i++;
continue;
}
/* Unknown option if we got to here */
fprintf(stderr,USAGE);
return 1;
}
 
/* Make sure there is one arg left */
 
if((i+1) != argc){
fprintf(stderr,USAGE);
return 1;
}
 
nrows = atoi(argv[i]);
 
/* Sanity checks */
if(nrows == 0){
if(cols_option)printf("0n");
exit(0); /* nothing to do */
}
if(nrows < 0){
fprintf(stderr,"%s: %d is an invalid number of rowsn",
PROGRAM_NAME,nrows);
exit(1);
}
if(nrows > MAX_ROWS){
fprintf(stderr,"%s: %d > %d rows maximumn",PROGRAM_NAME,nrows,
MAX_ROWS);
exit(1);
}
 
/* build the triangle */
 
triangle[0][0] = 1;
for(i=1;i<nrows;i++){
triangle[i][0] = 1;
for(j=0;j<i;j++)
triangle[i][j] = triangle[i-1][j-1]
+ triangle[i-1][j];
triangle[i][i] = 1;
}
 
/* Find the widest number in table. It's the middle one in
the bottom row */
 
max_width = num_digits(triangle[nrows-1][nrows/2]);
 
/* make sure this value is even */
if(max_width % 2) max_width++;
 
/* Since each number is printed in a field max_width+2 wide
and there are nrows numbers in the longest (bottom) row
the number of columns needed for the display is
(max_width+2)*nrows */
 
if(cols_option){
printf("%dn",(max_width + 2)*nrows);
exit(0);
}
 
/* Now print everything out */
 
for(i=0;i<nrows;i++){
 
/* This is the only tricky part. Think of each number as
being printed in the center of a brick. Each brick has
width max_width/2 + 1. Going down one row, the bricks
move over one half width */
 
row_offset = (nrows-i-1)*(max_width/2+1);
 
/* Move first row in deepest, etc., by printing the approp.
number of leading blanks */
 
for(j=0;j<row_offset;j++)printf(" ");
 
/* Print ith row of numbers */
 
for(j=0;j<i+1;j++)
if( print_centered(triangle[i][j],max_width+2)){
fprintf(stderr,"%s: error printing element %d,%dn",PROGRAM_NAME,i,j);
exit(1);
}
printf("n");
}
return 0;
}

¿Has encontrado algún error? ¿Tienes la solución? Dejame tu correción ;-)

Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress las eliminó al verlas como etiquetas HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios. Y lo mas importante: Todos los ejemplos fueron realizados por personas con únicamente conocimiento básico del lenguaje, no de programación.

Otro punto importante: Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:

  • Si es lenguaje C [c]Código en C[/c]
  • Si es lenguaje Pascal [pascal]Aquí dentro el código de Pascal[/pascal].

De esta manera el código coloreas el código.

Otro punto importante para muchos que sienten que se les ignora: Todos los comentarios los reviso y en su debido momento los apruebo, pero ojo con el con lo siguiente:

Me reservo el derecho de alterar, publicar o no los comentarios as´ como cambiar mis condiciones en el momento que así lo requiera.

¿estas de acuerdo? entonces adelante que ya te he quitado bastante tiempo leyendo esta basura de advertencias :)