Está en la página 1de 6

1

PROGRAMACIÓN II Y LABORATORIO
EJEMPLOS DE MATRICES
Ejemplo 1 :Cargue una matriz de mxn, obtenga en otra matriz la cantidad de dígitos de cada
elemento de la primera matriz, por ejm.:

0 1 2 0 1 2
0 8 123 12 0 1 3 2
15 1111 93 7 1 4 2 1
2 13 436 71192 2 2 3 5
3 92 4X3 3 2 4X3
2125 819 4 3

SOLUCIÓN:
PROCEDIMIENTO QUE CARGA DE ELEMENTOS UNA MATRIZ:
DISEÑO CODIFICACIÓN EN JAVA

cargar(A[][], f, c)
public static void cargar(int A[][], int f, int c)
{ int i,j;
i=0, f-1, 1 Scanner leer=new Scanner(System.in);
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
j=0, c-1,1 {
System.out.print("A["+(i+1)+"]["+(j+1)+"]=");
A[i][j]=leer.nextInt();
}
A[i][j] }
}

fin
2

PROCEDIMIENTO QUE MUESTRA UNA MATRIZ

mostrar(A[][], f, c)

public static void mostrar(int A[][], int f, int c)


{
i=0, f-1, 1 int i,j;
for(i=0;i<f;i++)
{
j=0, c-1, 1 for(j=0;j<c;j++)
{ System.out.print(A[i][j]+"\t");
}
System.out.println();
A[i][j] }
}

fin

PROCEDIMIENTO QUE CUENTA DÍGITOS DE CADA ELEMENTO DE UNA MATRIZ Y LO


ALMACENA EN OTRA MATRIZ

contarDigitos(A[][], D[][] f, c)
public static void contarDigitos(int A[][], int D[][], int f, int c )
{ int i,j;
d=1 for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
i=0, f-1, 1 { D[i][j]=(int)Math.log10(A[i][j])+1;
}
}
}
j=0,c-1, 1

D[i][j]=(int)log(A[i][j]) +1

fin
3

PROGRAMA PRINCIPAL
package ejemplo1_matrices;
INICIO import java.util.Scanner;

public class Ejemplo1_Matrices {

m,n // Incluir aquí los procedimientos anteriores

public static void main(String[] args) {


int m, n;
DIM A[m][n] Scanner leer=new Scanner(System.in);
DIM D[m][n] System.out.print("Número de filas: ");
m=leer.nextInt();
System.out.print("Número de columnas: ");
cargar(A, m,n) n=leer.nextInt();
int A[][]= new int[m][n];
contarDigitos(A,D,m,n)
int D[][]= new int[m][n];
mostrar(A,m,n) cargar(A, m,n);
mostrar(D,m,n) contarDigitos(A,D,m,n);
System.out.println("La matriz es:");
mostrar(A,m,n);
FIN System.out.println("La matriz con la cantidad de dígitos es:");
mostrar(D,m,n);
}
}

EJECUCIÓN DEL PROGRAMA:


4

Ejemplo 2: Cargue una matriz de mxn, muestre la matriz y obtenga la cantidad y suma de
números perfectos que contenga, por ejm.

0 1 2
0
9 6 18
1
13 8 28
2 6 3X3
36 3

Cantidad de números perfectos: 3


Suma de números perfectos: 40

NOTA: Realizar la función esPerfecto(x) que retorna 1 si x es perfecto y 0 si x no es


perfecto, un número x es perfecto si la suma de sus divisores excepto el mismo, es
igual al número x , por ejm.:
6 es un número perfecto, dado que las suma de sus divisores 1+2+3=6.

SOLUCIÓN:

FUNCIÓN QUE VERIFICA SI UN NÚMERO ES PERFECTO

public static int esPerfecto(int x)


esPerfectos(x) {
int i, s=0;
s=0 for(i=1; i<x; i++)
{ if(x%i==0)
s=s+i;
i=1, x-1, 1 }
if(x==s)
return 1;
return 0;
xmodi = 0 }

s=s + i

x=s

return 1

return 0
5

PROCEDIMIENTO QUE HALLA Y MUESTRA LA CANTIDAD Y SUMA DE NÚMEROS PERFECTOS DE


UNA MATRIZ

contarSumarPerfectos(A[][], f, c) public static void contarSumarPerfectos(int A[][],int f, int c)


{
int i,j, p;
d=0 int d=0, s=0;
for(i=0;i<f;i++)
s=0
{
for(j=0;j<c;j++)
i=0, f-1, 1 {
p=esPerfecto(A[i][j]);
if(p==1)
{ d++;
j=0,c-1, 1 s=s+A[i][j];
}
}
p= esPerfecto(A[i][j])
}
System.out.println("Cantidad de números perfectos:"+d);
System.out.println("Suma de números perfectos:"+s);
P=1 }

d=d+1
s=s + A[i][j]

d, s

FIN

PROGRAMA PRINCIPAL import java.util.Scanner;

INICIO public class Ejemplo2_Matrices {


// Escribir aquí los procedimientos y funciones
public static void main(String[] args) {
m,n int m, n;
Scanner leer=new Scanner(System.in);
System.out.print("Número de filas: ");
DIM B[m][n] m=leer.nextInt();
System.out.print("Número de columnas: ");
n=leer.nextInt();
int B[][]= new int[m][n];
cargar(B, m,n)
cargar(B, m,n);
mostrar(B,m,n)
System.out.println("La matriz es:");
contarSumarPerfectos(B,m,n)
mostrar(B,m,n);
contarSumarPerfectos(B,m,n);
}
FIN }
6

EJECUCIÓN

También podría gustarte