120 likes | 243 Views
OBJETIVOS. DEFINIR LA ESTRUCTURA DE ARREGLOS. DECLARAR ARREGLOS EN C. DESCRIBIR EL PROCESAMIENTO DE LOS ARREGLOS. CODIFICAR PROGRAMAS EN C UTILIZANDO LOS ARREGLOS. nombre. contenido. 3. 2. 0. 1. índice. notas [3]. ARREGLOS. Unidimensionales . . . . . . . Vectores. identificador.
E N D
OBJETIVOS • DEFINIR LA ESTRUCTURA DE ARREGLOS. • DECLARAR ARREGLOS EN C. • DESCRIBIR EL PROCESAMIENTO DE LOS ARREGLOS. • CODIFICAR PROGRAMAS EN C UTILIZANDO LOS ARREGLOS.
nombre contenido 3 2 0 1 índice notas [3] ARREGLOS • Unidimensionales . . . . . . . Vectores identificador notas Tamaño 4
LA DECLARACIÓN DE ARRAY UNIDIMENSIONAL • Se realiza según la siguiente sintaxis : • tipo de las variables nombre[ cantidad de elementos] ; • Por ejemplo : • int var1[10] ; • char nombre[50] ; • float numeros[200] ; • long double cantidades[25] ; float a[3]={2.45, -1.34, 2.11};
a[0] a[1] a[2] a[3] a[4] a[5] Indexación de arrays int a[6]; a[0]=23; a[2]=a[0]+5; a 23 28
3 2 0 1 • Declaración Formato: tipo de dato nombre_arreglo[tamaño] Ejm: int notas[4]; 2 bytes * 4 = 8 bytes char nombre[25]; int cantidad[10], totalest[5]; • Declaración e Inicialización char vocal[5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; float pu[3] = {2.50, 3.75, 5.00};
notas contenido + 1 + 1 + 1 3 2 0 1 índice: i = 0 i = i + 1 código i = 0 for(i=0;i<4;i++) { no i < 4 manejo = operaciones si i = i + 1 } • Acceso a un arreglo unidimensional int notas[4] 80 73 85 90 contador
notas 80 80 3 2 0 1 85 85 73 73 90 90 Declaración for(i=0;i<4;i++) int nota[4]; scanf("%d",¬as[i]); int i; • Manejo de vectores Lectura Pantalla Memoria 80 85 73 90 i
Contador Acumulador 80 73 90 85 notas i + 1 + 1 + 1 3 2 0 1 i = 0 suma = suma + notas[i] 80 no suma = 0; si for(i=0;i<4;i++) i < 4 suma = suma + notas[i] suma += notas[i]; 165 prom = suma / i ; 238 i = i + 1 prom 328 • Proceso [operaciones) Calcular el promedio. 0 328 238 80 165 3 1 2 0 4 i suma 82
80 73 90 85 notas i + 1 + 1 + 1 3 2 0 1 80 73 90 85 puts(“Las notas son:\n”); for(i=0;i<4;i++) printf(“%d \t”,notas[i]); • Impresión Memoria Pantalla Las notas son: Promedio: 82.00 printf(“\n\nPromedio:%.2f”, prom);
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; • VISUALIZACION: • for(i=0;i<10;i++) printf(“%d”,x[i]); x t 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
#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 (“\nLa media es %5.2f\n\n”, media); /* calcular la desviación y dar respuesta */ for ( cont=0; cont<n; ++cont) { d = lista[cont] - media; printf (“i= %d x= %5.2f d= %5.2f\n”, 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’,’d’,’y’,’s’,’\0’}*/ A d d y s \0 n n[0] n[1] n[2] n[3] n[4] n[5] Carácter nulo