Upload
ezekiel-wiggins
View
34
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
OBJETIVOS
• DEFINIR LA ESTRUCTURA DE ARREGLOS.
• DECLARAR ARREGLOS EN C.
• DESCRIBIR EL PROCESAMIENTO DE LOS ARREGLOS.
• CODIFICAR PROGRAMAS EN C UTILIZANDO LOS ARREGLOS.
ARREGLOSARREGLOS
• Unidimensionales . . . . . . . Vectores
identificador
notas
Tamaño 4
nombre
índice
notas [3]
0 1 2 3contenido
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};
Indexación de arrays
int a[6];
a[0]=23;
a[2]=a[0]+5;
a
a[0] a[1] a[2] a[3] a[4] a[5]
23 28
• Declaración
Formato: tipo de dato nombre_arreglo[tamaño]
Ejm: int notas[4];
0 1 2 3
2 bytes * 4 = 8 bytes
• Declaración e Inicialización
char vocal[5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};
float pu[3] = {2.50, 3.75, 5.00};
char nombre[25];int cantidad[10], totalest[5];
• Acceso a un arreglo unidimensional
int notas[4]
contenido0 1 2 3notas 80 85 73 90
+ 1 + 1 + 1
contador i = i + 1
índice: i = 0
i = 0
i = i + 1
i < 4
si
no
código
for(i=0;i<4;i++) {
manejo=
operaciones
}
• Manejo de vectores
Lectura
90
7385
80
Pantalla Memoria
80
85
73
90
notas
0 1 2 3
for(i=0;i<4;i++)
scanf("%d",¬as[i]);
80 85 73 90
Declaraciónint nota[4];int i;
i
prom
• Proceso [operaciones)
Calcular el promedio.
0 1 2 3notas 80 85 73 90
+ 1+ 1suma i
+ 1i
suma = suma + notas[i]
for(i=0;i<4;i++)
prom = suma / i ;
suma += notas[i];
suma = 0;
0
Contador
0
i = 0
i = i + 1
i < 4
si
no
suma = suma + notas[i]
82
80165238328
165238
328
80
Acumulador
123 4
• Impresión
Memoria
0 1 2 3notas 80 85 73 90
+ 1 + 1+ 1i
printf(“\n\nPromedio:%.2f”, prom);
for(i=0;i<4;i++) printf(“%d \t”,notas[i]);
puts(“Las notas son:\n”);
Las notas son:
Promedio: 82.00
Pantalla
80 85 9073
PROCESAMIENTO DE UN ARREGLOSe 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]);
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
x
t
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 \0n
n[0] n[1] n[2] n[3] n[4] n[5] Carácter nulo