String, arreglos y arreglos bidimensionales Diego Hernández R dhernand@inf.utfsm.cl
Hoy String Array Array bidimensionales
String U T F S M Un string es un array de caracteres. Ejemplo: ‘UTFSM’ Ejemplo: ‘Universidad’ U T F S M Palabra Variable: NOMBRE Índices 1 2 3 4 5 NOMBRE[2]=‘T’ longitud :=length(NOMBRE) ; longitud=5; true
Ejemplo String Program String; Uses Crt; Var s : String[10]; longitud: Integer; Begin s := 'UTFSM'; longitud := length(s) ; Writeln('longitud : ',longitud); if s[2]='T' then writeln('hola') else writeln('chao'); Readkey End.
Ejemplo 2 String Program String2; Uses Crt; Var s : String[3]; longitud: Integer; Begin s := 'UTFSM'; longitud := length(s) ; Writeln('longitud : ',longitud); if s[2]='T' then writeln('hola') else writeln('chao'); writeln(s); Readkey End.
Ejemplo 3 String Program String3; Uses Crt; Var s : String; d : String[5]; Begin s := 'UTFSM-Valparaiso-Chile'; writeln(s); Writeln('ingrese su nombre'); readln(s); writeln('Hola ',s); writeln('ingrese su apellido'); readln(d); writeln('Chao ',d); Readkey End. NOTA: cuando se usa STRING sin definir el largo este esta por defecto definido en 255
Array Similar al concepto de string, pero ahora el tipo de dato se puede definir. Sintaxis: Type {NUEVO TIPO DE DATO}=ARRAY[{INICIO INTERVALO}..{FIN INTERVALO}] of {TIPO DE DATOS DEFINIDO ANTERIORMENTE}
Ejemplo ARRAY Program Array; Uses Crt; Const MAXRANGO=10; Type nuevo=ARRAY[1..MAXRANGO] of Integer; Var n : nuevo; i : integer; Begin writeln('Ingrese 10 numeros porfavor:'); for i:=1 to MAXRANGO do read(n[i]); writeln(''); write(' ',n[i]); Readkey End.
ARRAY bidimensionales Es en tener ARRAY de ARRAY. Sintaxis: Type {NUEVO TIPO DE DATO}=ARRAY[{INICIO INTERVALO}..{FIN INTERVALO}, {INICIO INTERVALO}..{FIN INTERVALO}] of {TIPO DE DATOS DEFINIDO ANTERIORMENTE}
Ejemplo ARRAY bidimensionales Program Bidimensionales; Uses Crt; Const MAXRANGO=3; Type nuevo=ARRAY[1..MAXRANGO,1..MAXRANGO] of Integer; Var n : nuevo; i,j : integer; Begin writeln('Ingrese 9 numeros porfavor:'); for i:=1 to MAXRANGO do for j:=1 to MAXRANGO do read(n[i,j]); writeln(''); begin write(n[i,j],' '); writeln('') end; Readkey End.
EJERCICIO: Un arreglo de dos dimensiones con F filas y C columnas, contiene la cantidad de mensajes enviados por las personas de las filas a las personas de las columnas. La fig. muestra que la persona 3 ha enviado 20 mensajes a la persona 2. Escriba un programa para conocer la persona que ha enviado más mensajes y la persona que ha recibido más mensajes.
Ejercicios Construir una matriz de 5x5 llenarla en cada celda con el producto de los índices respectivos Solicitar Nombre y Apellidos, y luego entregarlos todos por pantalla juntos, pero permitir que el nombre guarde solo las 3 primeras letras Guardar los 100 primeros números primos en una ARRAY de tamaño 100
Fin ¿Alguna consulta?