OBJETIVOS DEFINIR LA ESTRUCTURA DE ARREGLOS DECLARAR ARREGLOS
OBJETIVOS • DEFINIR LA ESTRUCTURA DE ARREGLOS. • DECLARAR ARREGLOS EN C. • DESCRIBIR EL PROCESAMIENTO DE LOS ARREGLOS. • CODIFICAR PROGRAMAS EN C UTILIZANDO LOS ARREGLOS.
ARREGLOS • Unidimensionales. . . . Vectores nombre identificador notas contenido 0 1 2 3 índice Tamaño 4 notas [3]
LA DECLARACIÓN DE ARRAY UNIDIMENSIONAL • Se realiza según la siguiente sintaxis : tipo de las variables nombre[ cantidad de elementos] ; Por ejemplo : int var 1[10] ; char nombre[50] ; float numeros[200] ; long double cantidades[25] ; float a[3]={2. 45, -1. 34, 2. 11};
Indexación de arrays int a[6]; a[0]=23; a[2]=a[0]+5; a 23 28 a[0] a[1] a[2] a[3] a[4] a[5]
• Declaración Formato: tipo de dato nombre_arreglo[tamaño] Ejm: int notas[4]; 2 bytes * 4 = 8 bytes 0 char nombre[25]; int cantidad[10], totalest[5]; 1 2 • Declaración e Inicialización char vocal[5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; float pu[3] = {2. 50, 3. 75, 5. 00}; 3
• Acceso a un arreglo unidimensional notas 80 0 85 1 +1 73 2 +1 contador código +1 90 3 int notas[4] contenido índice: i = 0 i=i+1 i=0 for(i=0; i<4; i++) { manejo = operaciones no i<4 si i=i+1 }
• Manejo de vectores Lectura Pantalla Memoria notas 80 80 85 85 73 73 90 Declaración int nota[4]; int i; 80 i 0 85 73 90 1 2 3 90 for(i=0; i<4; i++) scanf("%d", ¬as[i]);
• Proceso [operaciones) Calcular el promedio. Acumulador Contador notas 80 85 73 90 3 2 0 1 i +1 +1 +1 0 165 328 238 80 41 0 3 2 suma i i=0 suma = suma + notas[i] suma = 0; for(i=0; i<4; i++) suma += notas[i]; prom = suma / i ; 80 165 238 328 82 prom no i<4 si suma = suma + notas[i] i=i+1
• Impresión Memoria notas 80 85 73 90 3 2 0 1 i +1 +1 +1 Pantalla Las notas son: 80 puts(“Las notas son: n”); for(i=0; i<4; i++) printf(“%d t”, notas[i]); printf(“nn. Promedio: %. 2 f”, prom); 85 73 90 Promedio: 82. 00
PROCESAMIENTO DE UN ARREGLO Se procesa elemento por elemento, mediante el uso de bucles. EJEMPLO: Bucle para almacenar los números del 0 al 9 en el arreglo x: . . . . int x[10]; for (t=0; t<10; t++) x[t] = t; 0 1 2 3 4 5 6 7 89 x t 0 1 2 3 4 5 6 7 8 9 VISUALIZACION: for(i=0; i<10; i++) printf(“%d”, x[i]); 0123456789
#include <stdio. h> /* Calculo de la media de n números y la desviación */ main ( ) { int n, cont; float media, d, suma = 0; float lista[100]; /* leer el valor de n */ printf (“Entre la cantidad de números para calcular la media”); scanf (“%d”, &n); printf (“n”); /* leer los números y calcular sumatoria */ for ( cont=0; cont<n; ++cont) { printf (“i= %d x=“, cont+1); scanf (“%f ”, &lista[cont]); suma += lista[cont]; } /* calcular la media y dar respuesta */ media = suma/n; printf (“n. La media es %5. 2 fnn”, media); /* calcular la desviación y dar respuesta */ for ( cont=0; cont<n; ++cont) { d = lista[cont] - media; printf (“i= %d x= %5. 2 f d= %5. 2 fn”, cont+1, lista[cont], d); } }
Strings • Los strings son los arrays de caracteres de una dimensión. Son las cadenas de caracteres. Definición: char n[6]=“Addys”; /*equivalente a char n[6]={’A’, ’d’, ’y’, ’s’, ’ ’}*/ n A d d y s n[0] n[1] n[2] n[3] n[4] n[5] Carácter nulo
- Slides: 12