You are on page 1of 25

ESCUELA INDUSTRIAL SUPERIOR

“PEDRO DOMINGO MURILLO”

INFORMÁTICA INDUTRIAL
PRIMER SEMESTRE
IYL-100 B

DOCENTE: LIC. ADRIAN QUISBERT VILELA


ESTUDIANTE: ROGER ALVARO MAMANI HUALLPA

LA PAZ- BOLIVIA
VECTORES

1. RELLENAR UN VECTOR V CON LOS PRIMEROS N NÚMEROS NATURALES

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);
}

public static void main(String arg[]) {


RellenarNatualesVector vec = new RellenarNatualesVector();
vec.principal();
}
}
2.-RELLENAR UN VECTOR P CON LOS PRIMEROS N NÚMEROS NATURALES
import java.util.Scanner;
public class RellenarNaturalesVector {
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);
}

public static void main(String arg[]) {


RellenarNatualesVector vec = new RellenarNatualesVector();
vec.principal();
}
}}

3.-SUMAR LOS ELEMENTOS DE UN VECTOR DE N POSICIONES


package vectores;
import java.util.Scanner;
public class VecSumarElementos {
Scanner lee = new Scanner(System.in);
int SumarElementos(int V[], int n) {
int i, S;
S = 0;
for (i = 0; i < n; i++) {
S = S + V[i];
}
return S;
}

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();
}

4.-SUMAR LAS POSICIONES PARES DE UN VECTOR DE N ELEMENTOS

import java.util.Scanner;

public class VecSumaPosPares {


Scanner lee = new Scanner(System.in);
int sumaPosPares(int V[], int n) {
int i, S;
S = 0;
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
S = S + V[i];
}
}
return S;
}
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 = sumaPosPares(V, n);
System.out.println("Mostrando Vector");
vec.mostrar(V, n);
System.out.println("La Suma es:" + S);
}
public static void main(String arg[]) {
VecSumaPosPares svec = new VecSumaPosPares();
svec.principal();
}
}
5.-HALLAR EL ELEMENTOS DE MENOR VALOR DE UN VECTOR DE N NÚMEROS

import java.util.Scanner;

public class VecBuscaMenor {


Scanner lee = new Scanner(System.in);

int buscaMenor(int V[], int n) {


int i, em;
em = V[0];
for (i = 1; i < n; i++) {
if (em > V[i]) {
em = V[i];
}
}
return em;
}
int buscaMayor(int V[], int n) {
int i, em;
em = V[0];
for (i = 1; i < n; i++) {
if (em < V[i]) {
em = V[i];
}
}
return em;
}

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);

public static void main(String arg[]) {


VecBuscaMenor svec = new VecBuscaMenor();
svec.principal();
}
}

6.-DADO UN VECTOR V DE DIMENSIÓN N, ORDENAR SUS ELEMENTOS EN


FORMA ASCENDENTE, APLICANDO EL MÉTODO BURBUJA.
public static void burbuja(int[] {
int i, j, n;
for (i = 0; i < n-2; i++) {
for (j = i+1 ;n- 1; j++) {
if (A[j + 1] < A[j]) {
aux = A[j + 1];
A[j + 1] = A[j];
A[j] = aux;
}
}
}
}

7.-DADOS DOS VECTORES A Y B DE DIMENSIÓN N, GENERAR OTRO VECTOR C =


A-B CON LA RESTA DE LOS ELEMENTOS DE LOS DOS VECTORES.
import java.util.Scanner;

public class RestaVectores {


Scanner lee = new Scanner(System.in);
void restaVector(int A[],int B[], int C[], int n)
{
int i;
for(i=0;i<n;i++)
C[i]= A[i]- B[i];
}
void principal() {
int n, A[],B[],C[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
A = new int[n];
System.out.println("Llenando vector A");
vec.llena(A, n);
B = new int[n];
System.out.println("Llenando vector B");
vec.llena(B, n);
C = new int [n];
sumaVector(A,B,C,n);
System.out.println("\nMostrando Vector A");
vec.mostrar(A, n);
System.out.println("\nMostrando Vector B");
vec.mostrar(B, n);
System.out.println("\nMostrando Vector C");
vec.mostrar(C, n);

public static void main(String arg[]) {


import java.util.Scanner;

public class RellenaFibonaci {


Scanner lee = new Scanner(System.in);
void rellenaFibonaci(int V[], int n)
{
int i;
V[0]=0;
V[1]=1;
for(i=2;i<n;i++)
{
V[i] = V[i-2]+ V[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];
rellenaFibonaci(V,n);
System.out.println("\nMostrando
Vector ");
vec.mostrar(V, n);
}

public static void main(String arg[]) {


RellenaFibonaci vec = new RellenaFibonaci();
vec.principal();
}
}

9.-CONTAR EL NÚMERO DE ELEMENTOS NEGATIVOS(EN LA VARIABLE CONT1),


CEROS(EN LA VARIABLE CONT2) Y POSITIVOS (EN LA VARIABLE CONT3), DE UN VECTOR
DE N POSICIONES

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);
}

public static void main(String


arg[]) {
ContarPosNegCeroVector vec = new ContarPosNegCeroVector();
vec.principal();
}
}

10.-CALCULAR LA MEDIA ARITMÉTICA DE LOS ELEMENTOS DE UN VECTOR DE N


POSICIONES

import java.util.Scanner;

public class VectorPromedio {

Scanner lee = new Scanner(System.in);

void promedio(int V[], int n) {


int i, s;
double p;
s = 0;
p = 0;
for (i = 1; i < n; i++) {
s = s + V[i];
}
p = (double) s / n;
System.out.println("el promedio es :" + p);
}

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);
}

public static void main(String arg[]) {


VectorPromedio vec = new VectorPromedio();
vec.principal();
}
}
11.-LLENAR UN VECTOR V DE N POSICIONES, CON LA SIGUIENTE SERIE: 3, 3 2, 33, 34, ., 3N

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);
}

public static void main(String arg[]) {


RellenarPot3Vec vec = new RellenarPot3Vec();
vec.principal();
}
}
12.-DADO UN VECTOR V DE DIMENSIÓN N, DETERMINAR LOS PUNTOS SILLA Y
MOSTRARLOS. SE CONSIDERA A UN ELEMENTO V(I) COMO PUNTO SILLA SI SE CUMPLE
LA SIGUIENTE CONDICIÓN: V(I-1) < V(I) < V(I+1).

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);
}

public static void main(String arg[]) {


PuntoSillaVec vec = new PuntoSillaVec();
vec.principal();
}
}

13.-BUSCAR UN DETERMINADO ELEMENTO X EN UN VECTOR DE N ELEMENTOS.

Scanner lee = new Scanner(System.in);

int busquedaSecuencial(int V[], int n, int X) {


int i;
int pos;
pos = -1;
for (i = 0; i < n; i++) {
if (V[i] == X) {
pos = i;
break;
}
}
return pos;
}

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);
}

public static void main(String arg[]) {


BusquedaSecuencialVec vec = new
BusquedaSecuencialVec();
vec.principal();
}
}

14.-TENIENDO DOS VECTORES A Y B DE N POSICIONES CADA UNO, A PARTIR DE ESTOS


CREAR OTRO VECTOR C QUE CONTENGA LOS ELEMENTOS TANTO DE A COMO DE B DE
FORMA INTERCALADA.

import java.util.Scanner;

public class IntercalarVector {


Scanner lee = new Scanner(System.in);
void intercalar(int A[],int B[], int C[], int n)
{
int i,j;
j=0;
for(i=0;i<n;i++){
C[j]= A[i];
C[j+1]= B[i];
j=j+2;
}
}
void principal() {
int n, A[],B[],C[];
Vector vec = new Vector();
System.out.println("Ingrese tamaño vector :");
n = lee.nextInt();
A = new int[n];
System.out.println("Llenando vector A");
vec.llena(A, n);
B = new int[n];
System.out.println("Llenando vector B");
vec.llena(B, n);
C = new int [n+n];
intercalar(A,B,C,n);
System.out.println("\nMostrando Vector A");
vec.mostrar(A, n);
System.out.println("\nMostrando Vector B");
vec.mostrar(B, n);
System.out.println("\nMostrando Vector C");
vec.mostrar(C, n+n);
}

public static void main(String arg[]) {


IntercalarVector svec = new IntercalarVector();
svec.principal();
}
}

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

public class Generar1 {

Scanner lee = new Scanner(System.in);

void generar1(int M[][], int f, int c) {

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);
}

public static void main(String arg[]) {


Generar1 gen = new Generar1();
gen.principal();
}
}}

2.- GENERAR LA SIGUIENTE MATRIZ RECTANGULAR ANXM, CON LOS SIGUIENTES


ELEMENTOS:
EJEMPLO. SI N=3X3

import iyl100_b_informatica.ProgramacionModular.GeneracionPrimos;
import java.util.Scanner;

public class GeneracioMatPrimos {

GeneracionPrimos genp = new GeneracionPrimos(); 2 3 5


Scanner lee = new Scanner(System.in);
7 11 13
int Nprimo(int n) {
int np, num, cp; 17 19 23
num = 2;

cp = 0;
np = 0;
while (cp < n) {
if (genp.verifica(num) == true) {
cp = cp + 1;
np = num;
}
num = num + 1;
}
return np;
}

void generarPrimo(int M[][], int f, int c) {


int i, j, p, cont;
cont = 1;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
p = Nprimo(cont);
M[i][j] = p;
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();
generarPrimo(M, f, c);
mat.mostrar(M, f, c);
}

public static void main(String arg[]) {


GeneracioMatPrimos gen = new GeneracioMatPrimos();
gen.principal();
}

3.- GENERAR LA SIGUIENTE MATRIZ RECTANGULAR A NXM, CON LOS


SIGUIENTES ELEMENTOS:
EJEMPLO. SI NXM=4X3

import
2 4 6

8 10 12

14 16 18

20 22 24

iyl100_b_informatica.ProgramacionModular.GeneracionPrimos;
import java.util.Scanner;

public class GeneraMatPares {


Scanner lee = new Scanner(System.in);

void generarPares(int M[][], int f, int c) {


int i, j, p, cont;
cont = 2;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
M[i][j] = cont;
cont = cont + 2;
}
}
}
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();
generarPares(M, f, c);
mat.mostrar(M, f, c);
}

public static void main(String arg[]) {


GeneraMatPares gen = new GeneraMatPares();
gen.principal();
}
}

4.-GENERAR LA SIGUIENTE MATRIZ RECTANGULAR DE ANXM


EJEMPLO. SI NXM=4X3
1 0 1
(0,0) (0,1) (0,2)

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;

public class GeneraMatAjedrez {

Scanner lee = new Scanner(System.in);


void generarTableroAjedrez(int M[][], int f, int c) {
int i, j;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
if ((i + j) % 2 == 0) {
M[i][j] = 1;
} else {
M[i][j] = 0;
}

}
}
}

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);
}

public static void main(String arg[]) {


GeneraMatAjedrez gen = new GeneraMatAjedrez();
gen.principal();
}
}

5.-GENERAR LA MATRIZ LATINA CUADRADA DE NXN Q


EJEMPLO. SI NXN=5X5

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;

public class GeneraMatLatina {


Scanner lee = new Scanner(System.in);

void generarLatina(int M[][], int f, int c) {


int i, j,cont;
for (i = 0; i < f; i++) {
cont = i +1;
for (j = 0; j < c; j++) {
M[i][j]=cont;
if (cont==f) {
cont = 1;
} else {
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();
generarLatina(M, f, c);
mat.mostrar(M, f, c);
}

public static void main(String arg[]) {


GeneraMatLatina gen = new GeneraMatLatina();
gen.principal();
}
}

6.-GENERAR LA SIGUIENTE MATRIZ RECTANGULAR DE ANXM


EJEMPLO. SI NXM=4X3

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

import java.util.Scanner;

public class GenerarMatFilas {


Scanner lee = new Scanner(System.in);
void generarFilas(int M[][], int f, int c) {
int i, j;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
M[i][j] = i+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();
generarFilas(M, f, c);
mat.mostrar(M, f, c);
}

public static void main(String arg[]) {


GenerarMatFilas gen = new GenerarMatFilas();
gen.principal();
}
}

7.-SUMAR LOS COMPONENTES DE UNA MATRIZ RECTANGULAR DE A NXM, EN UNA


VARIABLE SUMAM.
EJEMPLO. SI NXM=4X3

1 4 7

8 9 10

2 5 8

10 12 5

sumaM = 1 + 4 + 7+ 8+ 9+ 10 +2+ 5+ 8+ 10+12+5 = 67

import java.util.Scanner;

public class SumarMatElemento {


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[][];
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);
}

public static void main(String arg[]) {


SumarMatElemento gen = new SumarMatElemento();
gen.principal();
}
}

8.-HALLAR LA MEDIA ARITMÉTICA (PROMEDIO) DE LOS ELEMENTOS DE UNA


MATRIZ ANXM
NXM = 4 X 3 =12
1 4 7

8 9 10

2 5 8

10 12 5

sumaM = 1 + 4 + 7+ 8+ 9+ 10 +2+ 5+ 8+ 10+12+5 = 67


promedio = sumaM / (NxM) = 67 / 12 =5.58

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();
}
}

9.-HALLAR EL PRODUCTO DE LOS COMPONENTES DE UNA MATRIZ RECTANGULAR


DE ANXM, EN UNA VARIABLE SUMAM.
EJEMPLO. SI NXM=4X3

1 2 1

3 2 5

1 4 1

6 2 1

Entonces: sumaM = 1*2*1*3*2*5*1*4*1*6*2*1 = 2880

import java.util.Scanner;

public class MutiplicaMatElementos {

Scanner lee = new Scanner(System.in);

int multiplicaElemento(int M[][], int f, int c) {


int i, j, sumaM;
sumaM = 1;
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[][];
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);
}

public static void main(String arg[]) {


MutiplicaMatElementos gen = new MutiplicaMatElementos();
gen.principal();
}
}

10.-SUMAR LOS ELEMENTOS DE LA DIAGONAL SECUNDARIA DE UNA MATRIZ


CUADRADA ANX
EJEMPLO. SI NXN=4X4 LA MATRIZ A

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);

int sumaDiagonalSec(int M[][], int f, int c) {


int i, j, sumaM;
sumaM = 0;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
if(i==(f-(j+1)))
sumaM = sumaM + M[i][j];
}
}
return sumaM;
}

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);
}

public static void main(String arg[]) {


SumaMatDiagionalSec gen = new SumaMatDiagionalSec ();
gen.principal();
}
}

11.-GENERAR LA SIGUIENTE MATRIZ CUADRADA X DE ANXN


EJEMPLO. SI NXN=5X5

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;

public class GeneraMatX {


Scanner lee = new
Scanner(System.in);

void generaX (int M[][], int f,


int c) {
int i, j;
for (i = 0; i < f; i++) {
for (j = 0; j < c; j++) {
if((i==j)||(i==(f-(j+1))))
M[i][j]=1;
else
M[i][j]=0;
}
}
}

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);
}

public static void main(String arg[]) {


GeneraMatX gen = new GeneraMatX ();
gen.principal();
}
12.-Generar la siguiente matriz triangular superior de TSNxN
Ejemplo. Si NxM=4x3
I=2
J=2
1 1 1
0 1 1
0 0 1

package matrices;

import java.util.Scanner;

public class GeneraMatTrianSup {


Scanner lee = new Scanner(System.in);

void generaTrianSup (int M[][], int f, int c) {


int i, j;
for (i = 0; i < f; i++) {
for (j = i; j < c; j++) {
M[i][j]=1;
}
}
}

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.

DEBE SER PARA MATRICES CUADRADAS IMPARES


N= 3
Fil = 0
Col = N/2 = 3/2 = 1
Anterior fila , siguiente columna
Si es múltiplo de tamaño entonces siguiente fila

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;

public class MatCuboMagico {

Scanner lee = new Scanner(System.in);

void cuboMagico(int M[][], int n) {


int i, fil, col;
fil = 0;
col = n / 2;
Matriz mat = new Matriz();
for (i = 1; i <= n * n; i++) {
System.out.println("fil "+fil);
System.out.println("col "+col);
M[fil][col] = i;
if (i % n == 0) {
fil = fil + 1;
} else {
if (fil == 0) {
fil = n - 1;
} else {
fil = fil - 1;
}
if (col == n - 1) {
col = 0;
} else {
col = col + 1;
}
}

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);
}

public static void main(String arg[]) {


MatCuboMagico gen = new MatCuboMagico();
gen.principal();
}

You might also like