Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Programacin 1
InCo - FING
Contents
1 Tipo subrango 2
1.1 Subrangos de enteros . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Subrangos de caracteres . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Operaciones con subrangos . . . . . . . . . . . . . . . . . . . . . 3
1.4 Subrangos en general . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Arreglos 3
2.1 Tipos estructurados . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Motivacin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Declaracin de un arreglo . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Ejemplos de arreglos . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 Tipos Annimos . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6 Representacin de un arreglo en la memoria . . . . . . . . . . . . 5
2.7 Accediendo un arreglo . . . . . . . . . . . . . . . . . . . . . . . . 5
2.8 Inicializacin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9 Recorridas de arreglos . . . . . . . . . . . . . . . . . . . . . . . . 6
2.10 Bsqueda en un arreglo . . . . . . . . . . . . . . . . . . . . . . . 6
2.11 Bsqueda (2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.12 Bsqueda (3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.13 Bsqueda (4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.14 Evaluacin de booleanos . . . . . . . . . . . . . . . . . . . . . . . 8
1
2.15 Booleanos con circuito corto . . . . . . . . . . . . . . . . . . . . . 8
2.16 Bsqueda con circuito corto . . . . . . . . . . . . . . . . . . . . . 8
2.17 Bsqueda (5) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.18 Bsqueda (6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.19 Bsqueda contextual . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.20 Otro ejemplo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.21 Arreglos Multidimensionales . . . . . . . . . . . . . . . . . . . . . 10
1 Tipo subrango
type
(* tipos definidos por el programador *)
natural = 0..MaxInt;
decimal = 0..9;
mes = 1..12;
dia = 1..31;
otro = 1000..13890;
type
letra = A..Z;
digito = 0..9;
decimal = 0..9;
2
1.3 Operaciones con subrangos
2 Arreglos
Cada elemento del tipo est formado por un conjunto de valores (una estructura)
Tipos estructurados de Pascal:
Arreglos (array)
Registros (record)
Conjuntos (set)
3
2.2 Motivacin
a1 , a2 , ..., an
donde:
type
rango = 33..90;
arr1 = array [char] of integer; (* 256 celdas *)
arr2 = array [33..90] of real;
arr3 = array [integer] of char; (* demasiado grande! *)
arr4 = array [0..9] of arr3;(* matriz *)
arr5 = array [rango] of boolean;
4
type
(* indice anonimo *)
arreglo = array [0..9] of real;
var
(* arreglo anonimo *)
a : array [A..Z] of boolean;
Los tipos annimos se utilizan comnmente cuando un tipo slo aparece una vez.
const
MaxArreglo = 9;
type
RangoArreglo = 1..MaxArreglo;
Arreglo1 = array [RangoArreglo] of integer;
var
a : Arreglo1; (* ocupa 9 celdas *)
*------*------*------*------*-----*-----*-----*-----*-----*
| 23 | 34 | 0 | -12 | 6 | 9 | 11 | -2 | 34 |
*------*------*------*------*-----*-----*-----*-----*-----*
1 2 3 4 5 6 7 8 9
Cada celda del arreglo se puede acceder como una variable independiente.
Suponemos a del tipo array [1..10] of integer
(* asignaciones *)
a[1]:= 12;
a[10]:= 90;
....
a[i]:= i+1; (* i en el intervalo [1,10] *)
a[i+4]:= 20; (* i+4 en el intervalo [1,10] *)
a:= 0; (* error *)
5
2.8 Inicializacin
(* 2,4,6,...,20 *)
for i:= 1 to 10 do
a[i]:= 2 * i;
(* desde la entrada *)
for i:= 1 to 10 do
ReadLn(a[i]);
(* hallar el maximo *)
max:= a[1];
for i:= 2 to M do
if a[i] > max then
max:= a[i];
(* desplegar un arreglo *)
for i:= 1 to M do
WriteLn(a[i]);
i:= 1;
repeat
exito:= (a[i] = X);
i:= i+1;
until (i > M) or exito;
6
if exito then
WriteLn(exito)
else
WriteLn(fracaso)
Usando while
i:= 1;
exito:= false;
while (i<=M) and not exito do
begin
exito:= (a[i] = X);
i:= i+1;
end;
if exito then
WriteLn(exito)
else
WriteLn(fracaso)
(* INCORRECTO! *)
exito:= false;
for i:= 1 to M do
if (a[i] = X) then
exito:= true;
if exito then
WriteLn(exito)
else
WriteLn(fracaso)
7
exito:= false;
while (i<=M) and not exito do
begin
exito:= (a[i] = X);
i:= i+1;
end;
8
i:= 1;
(* circuito corto *)
while (i <= M) and (a[i] <> A) do
i:= i+1;
if i<=M then
WriteLn(exito)
else
WriteLn(fracaso);
i:=1;
while (i < M) and (a[i]<>A) do
i:= i+1;
if a[i] = A then
WriteLn(exito)
else
WriteLn(fracaso)
(* INCORRECTO! *)
i:= 1;
while (a[i] <> A) AND (i<=M) do
i:= i+1;
if i <= M then
WriteLn(exito)
else
WriteLn(fracaso);
9
2.19 Bsqueda contextual
i:=1;
(* circuito corto *)
while (i < M) and (a[i+1] <> A) do
i:= i+1;
if i < M then
WriteLn(exito: , a[i])
else
WriteLn(fracaso)
i:=1;
(* circuito corto *)
while (i <= M) and (a[i] mod 2 = 0) do
i:= i+1;
if i > M then
WriteLn(Son todos pares)
else
WriteLn(No todos son pares);
type
nombre = array [1..20, 1..15] of char;
datos = array [0..9, 1..10] of integer;
tabla = array [-10..10, 1..15] of char;
10
a[i][j]
a[i,j]
La segunda es la ms utilizada.
Mas sobre arreglos multidimensionales: Seccin 9.3 de Konvalina*
11