Laboratorio de INF-111
Vectores y matrices
Carlos Mullisaca Choque
[email protected] Universidad Mayor de San Andrés
Facultad de Ciencias Puras y Naturales
Carrera de Informática
Gestión 2017
CMC Laboratorio de INF-111 Vectores y matrices
Arrays
Un Array es una variable que puede contener varios datos del
mismo tipo
Para definir la variable:
double[] datos;
datos = new double [100];
double[] datos = new double [100];
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 1:vector
import java.io.*;
public class perfec
{public static void main(String[] args) throws IOException
{int p[]=new int[100];
int b=0, a,k,i,x,N;
BufferedReader dat = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Muestra los numeros Perfectos");
do{ System.out.print("Introduzca un numero mayor que
1 y menor a 2147483647: ");
N = Integer.parseInt( dat.readLine());
} while(N<=1);
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 1: vector
for(k=2;k<N;k++)
{ a=0;
for(i=1;i<(k+2);i++)
{if(k%i==0)
{ a=a+i;
if((a-k)==k)
{ p[b]=k;
b++;
}
}
}
}
for(x=0;x<b;x++)
{System.out.println(p[x]+"-Es Numero Perfecto");}
}}
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 2: vector
public class primos
{public static void main(String[] args)
{ int n, i, cnp = 0, res;
n = (int) (Math.random() * 100);
System.out.println("El vector almacena "+n+
"numeros aleatorios");
int num[] = new int[n + 1];
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 2: vector
for (i = 0; i < n; i++)
{num[i] = (int) (Math.random() * 100);
System.out.println(num[i]);
res = 0;
for (int j = 1; j <= num[i]; j++)
{ if (num[i] % j == 0)
{ res++; }
}
if (res == 2)
{ cnp++;
System.out.println(num[i]+"- Es numero primo");
}
}
System.out.println("La cantidad de numeros primos
es :" + cnp);
}
}
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 1: Matriz
public class c7_matriz
{ public static void main (String[] args)
{ int FILAS = 3;
int COLUMNAS = 5;
char letras[] [] = new char [FILAS] [COLUMNAS];
char letraQueToca = ’A’;
for (int f = 0 ; f < FILAS ; f++)
{ for (int c = 0 ; c < COLUMNAS ; c++)
{letras [f] [c] = letraQueToca;
letraQueToca++;
}
}
CMC Laboratorio de INF-111 Vectores y matrices
Ejemplo 1: Matriz
for (int f = 0 ; f < FILAS ; f++)
{ for (int c = 0 ; c < COLUMNAS ; c++)
{ System.out.print (letras[f][c] + " "); }
System.out.println ();
}
}
}
CMC Laboratorio de INF-111 Vectores y matrices
Práctica
Escribir un programa para introducir un número entero por
teclado y definir el tamaño del vector, almacenar con numeros
aleatorios el vector, halle y muestre el valor más alto que
contiene (su máximo) y el valor más bajo que contiene (su
mı́nimo).
CMC Laboratorio de INF-111 Vectores y matrices