ARREGLOS
alexgtInforme6 de Febrero de 2013
8.662 Palabras (35 Páginas)410 Visitas
ARREGLOS
Los arreglos permiten almacenar vectores y matrices. Los arreglos unidimensionales
Sirven para manejar vectores y los arreglos bidimensionales para matrices. Sin embargo, las matrices también se pueden almacenar mediante arreglos unidimensionales y por medio de apuntadores a apuntadores, temas que se verán en el capıtulo siguiente.
La palabra unidimensional no indica que se trata de vectores en espacios de dimensión uno; indica que su manejo se hace mediante un subíndice. El manejo de los arreglos bidimensionales se hace mediante dos sub-índices.
Un arreglo se caracteriza por ser una lista de numero finito de n. elementos del mismo tipo; almacenar los elementos del arreglo en memoria continua, tener un único nombre de variable que representa a todos elementos y estos se diferencian por un índice o subíndice, acceder de manera directa o aleatoria a los elementos individuales del arreglo, se clasifican en:
• UNIDIMENSIONALES: VECTORES OLISTAS
• BIDIMENSIONALES: TABLAS O MATRICES
• MULTIDIMENSIONALES
ARREGLOS UNIDIMENSIONALES (VECTORES OLISTAS)
ES UN CONJUNTO DE N. ELEMENTOS QUE CONTINUA EN UN VECTOR OLISTA. EJEMPLO:
Int b[5];
For(x=0;X<5;x++); {
Scanf (“%i”,&b[x]);
Printf(“\n); }
For(x=0; x<5; x++);
Printf(“\n %i”, b[x]);
ARREGLOS BIDIMENSIONALES
SON ESTRUCTURAS DE DATOS QUE AGRUPAN MUCHOS DATOS DEL MISMO TIPO, EN DONDE CADA ELEMENTO SE PUEDE TRABAJAR INDIVIDUALMENTE Y SE PUEDE CON UN MISMO NOMBRE. DECLARACION:
TIPO DE DATO, NOMBRE, MARIZ, [INDICE][SUB INDICE]
EJEMPLO:
Int [3][3];
For (x=0; x<3; x++){
For(y=0;y<3;y++)
Scanf(“%i”, & c[x][y]);
}
Char c[3][20];
Int x;
For(x=0;x<3;x++)
Scanf(“%s”,&c[x]);
Arreglo bidimensional para cadenas.
PROGRAMAS
Programa con menú de arreglo unidimensional captura, muestra, suma, promedia, copia arreglo, invierte arreglo, muestra mayor, muestra menor, busca un numero.
#include<conio.h>
#include<stdio.h>
#define p printf
#define s scanf
main()
{
int arre[5],opc,x,acum=0,arre2[5],may,men,bus,band,y;
float prom;
while(opc!=10)
{
p("\n 1-.capturar\n 2-.mostrar\n 3-.suma\n 4-.promedio\n 5-.copiar arreglo a2\n 6-.invertir arreglo\n 7-.mostrar mayor\n 8-.mostrar menor\n 9-. buscar numero\n 10-.salir "); s("%i",&opc);
switch(opc)
{
case 1: p("\n capturar ");
for(x=0;x<5;x++)
s("%i",&arre[x]);
break;
case 2: p("\n mostrar ");
for(x=0;x<5;x++)
p("\n %i",arre[x]);
break;
case 3: p("\n suma ");
for(x=0;x<5;x++)
acum=acum+arre[x];
p("\n %i",acum);
break;
case 4: p("\n promedio ");
prom=acum/5;
p("\n %.1f",prom);
break;
case 5: p("\n copiar arreglo ");
for(x=0;x<5;x++)
{
arre2[x]=arre[x];
p("\n %i\t%i",arre[x],arre2[x]);
}
break;
case 6: p("\n invertir arreglo");
y=0;
for(x=4;x>=0;x--)
{
arre2[y]=arre[x];
y++;
}
for(x=4;x>=0;x--)
p("\n %i ----------------------%i",arre2[x],x);
break;
case 7: p("\n mostrar mayor ");
may=arre[0];
for(x=0;x<5;x++)
{
if(arre[x]>may)
may=arre[x];
}
p("\n es mayor: %i",may);
break;
case 8: p("\n mostrar menor ");
men=arre[0];
for(x=0;x<5;x++)
{
if(arre[x]<men)
men=arre[x];
}
p("\n es menor: %i",men);
break;
case 9: p("\n buscar numero");
band=0;
p("\n que numero quiere buscar "); s("%i",&bus);
for(x=0;x<5;x++)
if(bus==arre[x])
{
p("\n numero %i encontrado",bus);
p("\n posicion es: %i",x);
band++;
}
if(band==0)
p("\n el numero %i no se encontro",bus);
case 10:
p("\n fin programa");
break;
default: p("\n opcion incorrecta");
}
}
getch();
return 0;
}
Programa con menú de arreglos bidimensionales capturar, mostrar, buscar un numero, suma n columna, suma n fila, invertir filas por columnas, invertir, suma de toda la matriz, multiplicar por un vector, multiplicar por un numero, mostrar diagonal, sumar diagonal, salir.
#include<conio.h>
#include<stdio.h>
#define p printf
#define s scanf
main()
{
int m[3][3],x,y,z,opc,band,bus,fil,colu,acum,a[3],m3[3][3]={0,0,0,0,0,0,0,0,0},num,diag;
while(opc!=13)
{
p("\n 1-.capturar\n 2-.mostrar\n 3-.buscar un numero\n 4-.suma n columna\n 5-.suma n fila\n 6-.invertir filas por columnas\n 7-.invertir\n 8-. suma de toda la matriz\n 9-. multiplicar por un vector\n 10-.multiplicar por un numero\n 11-.mostrar diagonal\n 12-.sumar diagonal\n
...