Triangulo de Pascal

Triangulo de Pascal

¿Has encontrado un error? ¿Tienes la solución? Deja 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 estudiante con únicamente conocimiento básico del lenguaje, no de programación.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* 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 displaycolumns 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 inthe 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;
}

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

  • Si es lenguaje C <code lang="c">Código en C</code>
  • Si es lenguaje Pascal <code lang="pascal">Aquí dentro el código de Pascal</code>.

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

Deja un comentario

Suscribirse a los comentarios.