Professional Documents
Culture Documents
VECTORES
VECTORES
INFORMÁTICA INDUTRIAL
PRIMER SEMESTRE
IYL-100 B
LA PAZ- BOLIVIA
VECTORES
import java.util.Scanner;
public class RellenarNatualesVector {
Scanner lee = new Scanner(System.in);
void rellenarNaturales(int V[], int n)
{
int i;
for(i=0;i<n;i++)
V[i] = i+1;
}
void principal() {
int n, V[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
V = new int[n];
rellenarNaturales(V,n);
System.out.println("\nMostrando Vector ");
vec.mostrar(V, n);
}
void principal() {
int n, S, V[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
V = new int[n];
System.out.println("Llenando vector");
vec.llena(V, n);
S = SumarElementos(V, n);
System.out.println("Mostrando Vector");
vec.mostrar(V, n);
System.out.println("La Suma es:" + S);
}
public static void main(String arg[]) {
VecSumarElementos svec = new
VecSumarElementos();
svec.principal();
}
import java.util.Scanner;
import java.util.Scanner;
void principal() {
int n, emen,emay, V[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
V = new int[n];
System.out.println("Llenando vector");
vec.llena(V, n);
emen = buscaMenor(V, n);
emay = buscaMayor(V, n);
System.out.println("Mostrando Vector");
vec.mostrar(V, n);
System.out.println("elemento menor:" + emen);
System.out.println("elemento mayor:" + emay);
import java.util.Scanner;
public class ContarPosNegCeroVector {
Scanner lee = new Scanner(System.in);
void contarPosNegCero(int V[], int n) {
int i, cont1, cont2, cont3;
cont1 = 0;
cont2 = 0;
cont3 = 0;
for (i = 0; i < n; i++) {
if (V[i] == 0) {
cont1 = cont1 + 1;
} else if (V[i] > 0) {
cont2 = cont2 + 1;
} else {
cont3 = cont3 + 1;
}
}
System.out.println("Total Ceros
=" + cont1);
System.out.println("Total
Positivos=" + cont2);
System.out.println("Total
Negativos =" + cont3);
}
void principal() {
int n, V[];
Vector vec = new Vector();
System.out.println("Ingrese
tamaño vector :");
n = lee.nextInt();
V = new int[n];
vec.llena(V, n);
System.out.println("\
nMostrando Vector ");
vec.mostrar(V, n);
contarPosNegCero(V,n);
}
import java.util.Scanner;
void principal() {
int n, V[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
V = new int[n];
vec.llena(V, n);
promedio(V, n);
System.out.println("\nMostrando Vector ");
vec.mostrar(V, n);
}
import java.util.Scanner;
public class RellenarPot3Vec {
Scanner lee = new Scanner(System.in);
void rellenarPot3(int V[], int n)
{
int i;
for(i=0;i<n;i++)
{
int p;
p=(int)Math.pow(3, (i+1));
V[i] = p;
}
}
void principal() {
int n, V[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
V = new int[n];
rellenarPot3(V,n);
System.out.println("\nMostrando Vector ");
vec.mostrar(V, n);
}
import java.util.Scanner;
public class PuntoSillaVec {
Scanner lee = new
Scanner(System.in);
void mostrarPuntosSilla(int V[], int n)
{
int i;
for(i=1;i<n-1;i++)
{
if((V[i] < V[i+1]) && (V[i] > V[i-1]))
{
System.out.println("Los puntos
sillas son "+V[i]);
}
}
}
void principal() {
int n, V[];
Vector vec = new Vector();
System.out.println("Ingrese
tamaño vector :");
n = lee.nextInt();
V = new int[n];
vec.llena(V,n);
mostrarPuntosSilla(V,n);
System.out.println("\nMostrando
Vector ");
vec.mostrar(V, n);
}
void principal() {
int n, V[], pos, X;
Vector vec = new Vector();
System.out.println("Ingrese tamaño
vector :");
n = lee.nextInt();
V = new int[n];
vec.llena(V, n);
System.out.println("Elemento a
buscar");
X = lee.nextInt();
pos = busquedaSecuencial(V, n, X);
if (pos >= 0) {
System.out.println("el elemento " +
X + "se ha encontrado en la posicion " +
(pos+1));
} else {
System.out.println("no existe el
elemento " + X);
}
System.out.println("\nMostrando
Vector ");
vec.mostrar(V, n);
}
import java.util.Scanner;
MATRICES
1.-GENERAR LA SIGUIENTE MATRIZ RECTANGULAR ANXM, CON LOS SIGUIENTES
ELEMENTOS
package matrices;
1 2 3
4 5 6
import java.util.Scanner; 7 8 9
10 11 12
int i, j, cont;
cont = 1;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
M[i][j] = cont;
cont = cont + 1;
}
}
}
void principal() {
int f, c;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
generar1(M, f, c);
mat.mostrar(M, f, c);
}
import iyl100_b_informatica.ProgramacionModular.GeneracionPrimos;
import java.util.Scanner;
cp = 0;
np = 0;
while (cp < n) {
if (genp.verifica(num) == true) {
cp = cp + 1;
np = num;
}
num = num + 1;
}
return np;
}
void principal() {
int f, c;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
generarPrimo(M, f, c);
mat.mostrar(M, f, c);
}
import
2 4 6
8 10 12
14 16 18
20 22 24
iyl100_b_informatica.ProgramacionModular.GeneracionPrimos;
import java.util.Scanner;
0 1 0
(1,0) (1,1) (1,2)
1 0 1
(2,0) (2,1) (2,2)
0 1 0
(3,0) (3,1) (3,2)
import java.util.Scanner;
}
}
}
void principal() {
int f, c;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
generarTableroAjedrez(M, f, c);
mat.mostrar(M, f, c);
}
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
import java.util.Scanner;
void principal() {
int f, c;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
generarLatina(M, f, c);
mat.mostrar(M, f, c);
}
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
import java.util.Scanner;
}
}
}
void principal() {
int f, c;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
generarFilas(M, f, c);
mat.mostrar(M, f, c);
}
1 4 7
8 9 10
2 5 8
10 12 5
import java.util.Scanner;
void principal() {
int f, c,sm;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
mat.llenar(M, f, c);
sm=sumarElemento(M, f, c);
mat.mostrar(M, f, c);
System.out.println("La suma de elementos es:"+sm);
}
8 9 10
2 5 8
10 12 5
import java.util.Scanner;
public class PromedioMatElementos {
Scanner lee = new Scanner(System.in);
int sumarElemento(int M[][], int f, int c) {
int i, j,sumaM;
sumaM=0;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
sumaM=sumaM+M[i][j];
}
}
return sumaM;
}
void principal() {
int f, c,sm;
int M[][];
double promedio;
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
mat.llenar(M, f, c);
sm=sumarElemento(M, f, c);
mat.mostrar(M, f, c);
promedio = (double)sm / (f*c);
System.out.println("El promedio es es:"+promedio);
}
public static void main(String arg[]) {
PromedioMatElementos gen = new PromedioMatElementos();
gen.principal();
}
}
1 2 1
3 2 5
1 4 1
6 2 1
import java.util.Scanner;
void principal() {
int f, c, sm;
int M[][];
System.out.println("Numero de filas:");
f = lee.nextInt();
System.out.println("Numero de columnas:");
c = lee.nextInt();
M = new int[f][c];
Matriz mat = new Matriz();
mat.llenar(M, f, c);
sm = multiplicaElemento(M, f, c);
mat.mostrar(M, f, c);
System.out.println("La multiplicacion de elementos es:" + sm);
}
1 2 5 4
(0,3)
6 0 2 1
(1,2)
0 10 1 7
(2,1)
1 3 3 4
(3,0)
Entonces la suma de sus
elementos:
sumaA= 4+2+10+1 = 17
N= 4
If ( i = N-(j+1) ) then s = s+ M[I,j]
If( 0 = 4 – (3+1)) then s= 0 + 4
If( 1 = 4 – (2+1)) then s= 4+ 2
public class SumaMatDiagionalSec {
Scanner lee = new Scanner(System.in);
void principal() {
int n, sm;
int M[][];
System.out.println("Tamaño de la Matriz");
n = lee.nextInt();
M = new int[n][n];
Matriz mat = new Matriz();
mat.llenar(M, n, n);
sm = sumaDiagonalSec(M, n, n);
mat.mostrar(M, n, n);
System.out.println("La suma de la diogonal :" + sm);
}
1 0 0 0 1
(0,0)
0 1 0 1 0
(1,1)
0 0 1 0 0
(2,2)
0 1 0 1 0
(3,3)
1 0 0 0 1
(4,4)
import java.util.Scanner;
void principal() {
int n, sm;
int M[][];
System.out.println("Tamaño de la Matriz");
n = lee.nextInt();
M = new int[n][n];
Matriz mat = new Matriz();
generaX(M, n, n);
mat.mostrar(M, n, n);
}
package matrices;
import java.util.Scanner;
void principal() {
int n, sm;
int M[][];
System.out.println("Tamaño matriz");
n = lee.nextInt();
M = new int[n][n];
Matriz mat = new Matriz();
generaTrianSup(M, n, n);
mat.mostrar(M, n, n);
}
public static void main(String arg[]) {
GeneraMatTrianSup gen = new GeneraMatTrianSup ();
gen.principal();
}
}
]
13DADA UNA MATRIZ A CUADRADA, GENERAR EL CUBO MÁGICO.
8 1 6
3 5 7
4 9 2
N=5
Fil = 0
Col = n/2 => 5/2 = 2
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
import java.util.Scanner;
mat.mostrar(M, n, n);
}
}
void principal() {
int n;
int M[][];
System.out.println("Tamaño de la matriz ");
n = lee.nextInt();
M = new int[n][n];
Matriz mat = new Matriz();
cuboMagico(M, n);
mat.mostrar(M, n, n);
}