0% found this document useful (0 votes)
63 views9 pages

Dadas Dos Matrices A y B Intercambiar Los Minimos de A Con Los Maximos de B

The document contains code to perform several operations on matrices in Java: 1) Exchange the minimum values of matrix A with the maximum values of matrix B. It finds the minimum of A and maximum of B, then replaces matching values in the matrices. 2) Invert the main diagonal of a square matrix by swapping the value in position (i,i) with the value in position (n,n) where n is the dimension. 3) Invert the secondary diagonal of a square matrix by swapping each value with the value across the diagonal. 4) Find the common elements between matrices R and S of different sizes and print them. It compares each element of R to each element of S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views9 pages

Dadas Dos Matrices A y B Intercambiar Los Minimos de A Con Los Maximos de B

The document contains code to perform several operations on matrices in Java: 1) Exchange the minimum values of matrix A with the maximum values of matrix B. It finds the minimum of A and maximum of B, then replaces matching values in the matrices. 2) Invert the main diagonal of a square matrix by swapping the value in position (i,i) with the value in position (n,n) where n is the dimension. 3) Invert the secondary diagonal of a square matrix by swapping each value with the value across the diagonal. 4) Find the common elements between matrices R and S of different sizes and print them. It compares each element of R to each element of S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Dadas dos matrices A y B intercambiar los minimos de A con los maximos de B

1 import java.util.Scanner;
2
3 public class JavaMatrices1 {
4
Scanner Leer = new Scanner(System.in);
5
6 void llenar (int M [] [], int f, int c)
7 {
8
9 for (int i = 1 ; i <= f ; i++)
10 {
11 for (int j = 1 ; j <= c ; j++)
{
12 System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
13 M [i] [j] = Leer.nextInt();
14 }
15 }
}
16
17
18
void mostrar (int M [] [], int f, int c)
19 {
20 for (int i = 1 ; i <= f ; i++)
21 {
22 System.out.println ();
for (int j = 1 ; j <= c ; j++)
23 {
24 System.out.print ("[" + M [i] [j] + "]");
25 }
26 }
27 }
28
29
int menor (int M [] [], int f, int c)
30 {
31 int men = M [1] [1];
32 for (int i = 1 ; i <= f ; i++)
33 {
34 for (int j = 1 ; j <= c ; j++)
{
35 if (M [i] [j] < men)
36 men = M [i] [j];
37 }
38 }
return (men);
39
}
40
41
42 int maximo (int M [] [], int f, int c)
43 {
44 int max = M [1] [1];
45 for (int i = 1 ; i <= f ; i++)
{
46
for (int j = 1 ; j <= c ; j++)
47 {
48 if (M [i] [j] < max)
49 max = M [i] [j];
50 }
}
51
52 return (max);
}
53
54
void intercambiar (int A [] [], int fa, int ca, int B [] [], int fb, int cb)
55 {
56 int min_a = menor (A, fa, ca);
57 int max_b = maximo (B, fb, cb);
58
59 //para cambiar los minimos de A con los maximos de B
for (int i = 1 ; i <= fa ; i++)
60 {
61 for (int j = 1 ; j <= ca ; j++)
62 {
63 if (A [i] [j] == min_a)
64 A [i] [j] = max_b;
}
65 }
66
67 //para intercambiar los maximos de con los minimos de A
68 for (int i = 1 ; i <= fb ; i++)
69 {
70 for (int j = 1 ; j <= cb ; j++)
{
71 if (B [i] [j] == max_b)
72 B [i] [j] = min_a;
73 }
74 }
}
75
76
public static void main (String args [])
77 {
80 Scanner Leer = new Scanner(System.in);
81 JavaMatrices1 h = new JavaMatrices1 ();
82 int A [] [] = new int [20] [20];
int B [] [] = new int [20] [20];
83 System.out.print ("Insert filas de A: ");
84 int fa = Leer.nextInt();
85 System.out.print ("Insert columnas de A: ");
86 int ca = Leer.nextInt();
87 System.out.print ("Insert filas de B: ");
int fb = Leer.nextInt();
88 System.out.print ("Insert columnas de B: ");
89 int cb = Leer.nextInt();
90
91 //lectura de matrices
92 System.out.println ("nINSERTANDO DATOS EN MATRIZ A: n");
93 h.llenar (A, fa, ca);
System.out.println ("nINSERTANDO DATOS EN MATRIZ B: n");
94 h.llenar (B, fb, cb);
95 System.out.println ("nMATRICES ORIGINALMENTE INSERTADAS: ");
96 h.mostrar (A, fa, ca);
97 System.out.println ();
h.mostrar (B, fb, cb);
98 System.out.println ();
99
100 //intercambiando elementos
101 h.intercambiar (A, fa, ca, B, fb, cb);
102 System.out.println ("nMATRICES DESPUES DEL INTERCAMBIO:");
103 h.mostrar (A, fa, ca);
System.out.println ();
104 h.mostrar (B, fb, cb);
105
106
1. Dada una matriz cuadrada invertir su diagonal principal

1 import java.util.Scanner;
2
3 public class JavaMatrices2 {
4
Scanner Leer = new Scanner(System.in);
5
6 void llenar (int M [] [], int d)
7 {
8 for (int i = 1 ; i <= d ; i++)
9 {
for (int j = 1 ; j <= d ; j++)
10
{
11 System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
12 M [i] [j] = Leer.nextInt();
13 }
14 }
}
15
16
17
void mostrar (int M [] [], int d)
18 {
19 for (int i = 1 ; i <= d ; i++)
20 {
21 System.out.println ();
for (int j = 1 ; j <= d ; j++)
22 {
23 System.out.print ("[" + M [i] [j] + "]");
24 }
25 }
26 }
27
28
void invierte (int M [] [], int d)
29 {
30 int fin = d;
31 for (int i = 1 ; i <= d / 2 ; i++)
32 {
33 int aux = M [i] [i];
M [i] [i] = M [d] [d];
34 M [d] [d] = aux;
35 fin--;
36 }
37 }
38
39
40
41 public static void main (String args [])
{
42 Scanner Leer = new Scanner(System.in);
43 JavaMatrices2 h = new JavaMatrices2 ();
44 int M [] [] = new int [20] [20];
45 System.out.print ("Inserte dimen. de la matriz cuadrada: ");
int d = Leer.nextInt();
46
h.llenar (M, d);
47 System.out.print ("nMATRIZ ORIGINAL: ");
48 h.mostrar (M, d);
49 System.out.print ("nnMATRIZ CON LA DIAGONAL PRINCIPAL INVERTIDA: ");
h.invierte (M, d);
5 h.mostrar (M, d);
}
2. Dada una matriz cuadrada invertir su diagonal secundaria

1
2 import java.util.Scanner;
3
public class JavaMatrices3 {
4
5 Scanner Leer = new Scanner(System.in);
6
7 void llenar (int M [] [], int d)
8 {
9 for (int i = 1 ; i <= d ; i++)
10 {
for (int j = 1 ; j <= d ; j++)
11 {
12 System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
13 M [i] [j] = Leer.nextInt();
14 }
15 }
}
16 void mostrar (int M [] [], int d)
17 {
18 for (int i = 1 ; i <= d ; i++)
19 {
System.out.println ();
20 for (int j = 1 ; j <= d ; j++)
21 {
22 System.out.print ("[" + M [i] [j] + "]");
23 }
24 }
}
25 void invierte (int M [] [], int d)
26 {
27 int fin = d;
28 for (int i = 1 ; i <= d / 2 ; i++)
{
29
int aux = M [i] [d];
30 M [i] [d] = M [d] [i];
31 M [d] [i] = aux;
32 fin--;
33 }
}
34 public static void main (String args [])
35 {
36 Scanner Leer = new Scanner(System.in);
37 JavaMatrices3 h = new JavaMatrices3 ();
38 int M [] [] = new int [20] [20];
System.out.print ("Inserte dimen. de la matriz cuadrada: ");
39 int d = Leer.nextInt();
40 h.llenar (M, d);
41 System.out.print ("nMATRIZ ORIGINAL: ");
42 h.mostrar (M, d);
System.out.print ("nnMATRIZ CON LA DIAGONAL SECUNDARIA INVERTIDA: ");
43 h.invierte (M, d);
44 h.mostrar (M, d);
45 }
46
1. Dada dos matrices de diferentes tamaos R y S mostrar los elementos comunes de R en S

1 import java.util.Scanner;
2
3 public class JavaMatrices4 {
4
Scanner Leer = new Scanner(System.in);
5
6 void llenar (int M [] [], int f, int c)
7 {
8 for (int i = 1 ; i <= f ; i++)
9 {
for (int j = 1 ; j <= c ; j++)
10
{
11 System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
12 M [i] [j] = Leer.nextInt();
13 }
14 }
}
15 void mostrar (int M [] [], int f, int c)
16 {
17 for (int i = 1 ; i <= f ; i++)
18 {
19 System.out.println ();
for (int j = 1 ; j <= c ; j++)
20 {
21 System.out.print ("[" + M [i] [j] + "]");
22 }
23 }
}
24
void comunes (int R [] [], int fr, int cr, int S [] [], int fs, int cs)
25 {
26 System.out.print ("nnLos elementos comunes de R en S son: ");
27 for (int i = 1 ; i <= fr ; i++)
28 {
for (int j = 1 ; j <= cr ; j++)
29 {
30 for (int k = 1 ; k <= fs ; k++)
31 {
32 for (int l = 1 ; l <= cs ; l++)
33 {
if (R [i] [j] == S [k] [l])
34 System.out.print ("[" + R [i] [j] + "]");
35 }
36 }
37 }
}
38 }
39 public static void main (String args [])
40 {
41 Scanner Leer = new Scanner(System.in);
42 JavaMatrices4 h = new JavaMatrices4 ();
int R [] [] = new int [20] [20];
43 int S [] [] = new int [20] [20];
44 System.out.print ("Inserte filas de R: ");
45 int fr = Leer.nextInt();
46 System.out.print ("Inserte columnas de R: ");
47 int cr = Leer.nextInt();
System.out.print ("Inserte filas de S: ");
48 int fs = Leer.nextInt();
49 System.out.print ("Inserte columnas de S: ");
50 int cs = Leer.nextInt();
51
System.out.print ("nLLENANDO MATRIZ R: n");
52 h.llenar (R, fr, cr);
53 System.out.print ("nLLENANDO MATRIZ S: n");
54 h.llenar (S, fs, cs);
55 System.out.print ("nLA MATRICES R : ");
56 h.mostrar (R, fr, cr);
System.out.print ("nLA MATRICES S : ");
57 h.mostrar (S, fs, cs);
58 h.comunes (R, fr, cr, S, fs, cs);
59 }
6
1. Dada la matriz de m*n y el vector de tamao n, determinar que columna de la matriz es igual al vector

1 import java.util.Scanner;
2
3 public class JavaMatrizVector1 {
4
Scanner Leer = new Scanner(System.in);
5
6 void llenarMatriz (int M [] [], int f, int c)
7 {
8 for (int i = 1 ; i <= f ; i++)
9 {
for (int j = 1 ; j <= c ; j++)
10
{
11 System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
12 M [i] [j] = Leer.nextInt();
13 }
14 }
}
15 void mostrarMatriz (int M [] [], int f, int c)
16 {
17 for (int i = 1 ; i <= f ; i++)
18 {
19 System.out.println ();
for (int j = 1 ; j <= c ; j++)
20 {
21 System.out.print ("[" + M [i] [j] + "]");
22 }
23 }
}
24
void llenarVector (int V [], int d)
25 {
26 for (int i = 1 ; i <= d ; i++)
27 {
28 System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.nextInt();
29 }
30 }
31
32
33 void mostrarVector (int V [], int d)
34 {
35 for (int i = 1 ; i <= d ; i++)
{
36 System.out.print ("[" + V [i] + "]");
37 }
38 }
39 void procedure (int M [] [], int f, int c, int V [], int d)
40 {
for (int i = 1 ; i <= f ; i++)
41 {
42 int sw = 1;
43 for (int j = 1 ; j <= c ; j++)
44 {
45
for (int k = 1 ; k <= d ; k++)
46
{
47 if (M [j] [i] != V [k])
48 sw = 0;
49 }
50
51
52
53
54
55
56
57
}
58 if (sw == 1)
59 System.out.println ("nnLa columna " + i + " es igual al vector");
60 }
61 }
public static void main (String args [])
62 {
63 Scanner Leer = new Scanner(System.in);
64 JavaMatrizVector1 h = new JavaMatrizVector1();
65 int M [] [] = new int [20] [20];
66 int V [] = new int [20];
System.out.print ("Inserte filas de la matriz: ");
67 int f = Leer.nextInt();
68 System.out.print ("Inserte dimension del vector: ");
69 int d = Leer.nextInt();
70
71 System.out.print ("nLLENANDO MATRIZ: n");
h.llenarMatriz(M, f, d);
72
System.out.print ("nLLENANDO EL VECTOR: n");
73 h.llenarVector (V, d);
74
75
76 System.out.print ("nLA MATRIZ: ");
77 h.mostrarMatriz(M, f, d);
78 System.out.print ("nnEL VECTOR: n");
h.mostrarVector (V, d);
79
80
h.procedure (M, f, d, V, d);
81 }
82
83
84
85
86
87
88
89

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy