Documentos de Académico
Documentos de Profesional
Documentos de Cultura
INSTITUTO TECNOLÓGICO
“MARCELO QUIROGA SANTA CRUZ”
CUARTA ETAPA
CAPÍTULO VI
CONTINUACIÓN ARREGLOS
ARREGLOS BIDIMENSIONALES -MATRICES
DEFINICIÓN : Una matriz o arreglo bidimensional es una estructura de datos que permite
agrupar datos de un mismo tipo bajo un mismo nombre, organizándolos en filas y columnas.
Ejm1.: La siguiente figura muestra una matriz rectangular de nombre A y dimensión 4x3 (4
filas y 3 columnas ) que almacena datos de tipo numérico entero
A
0 1 2
0 8 3 12 Matriz rectangular :
filas Es una matriz que
1 35 1 0
tiene diferente
2 3 14 3 número de filas y
3 92 8 123 4X3 columnas
columnas
Ejm2.: La siguiente figura muestra una matriz cuadrada de nombre M y dimensión 3x3 (3
filas y 3 columnas ) que almacena datos de tipo caracter.
M
0 1 2
0 c d q Matriz cuadrada : Es
una matriz que tiene
1 j p t el mismo número de
2 x a e 3x3 filas y columnas
A 0 1 2
X
Por lo tanto, la asignación de elementos 3 Después de la asignación de elementos, la
en cada posición de matriz A, se realizaría matriz A queda cargada de la siguiente forma:
de la siguiente forma:
A
0 1 2
A[0,0] = 9
A[0,1] =3 0 9 3 5
A[0,2] = 5 1 82 1 0
A[1,0] = 82
2 3 69 3
A[1,1] = 1
A[1,2] = 0 3 10 8 49 4X3
A[2,0] = 3
A[2,1] = 69
A[2,2] = 3
A[3,0] = 10
A[3,1] = 8
A[3,2] = 49
RECORRIDO HABITUAL EN UNA MATRIZ. El recorrido usual que se realiza en una matriz, es
el recorrido por filas, es decir para llenar, mostrar y realizar otro tipo de operaciones en la
misma, primero se recorre la primera fila de izquierda a derecha, luego la segunda fila, y así
sucesivamente hasta llegar a la última fila, como se puede observar en el ejemplo anterior..
Ejemplo 1: Dimensionar una matriz de 4x3, llenar todas las posiciones de la misma con el
número 8 y mostrar la matriz, es decir:
i j j
A[0,0] = 8
A[0,1] =8 A 0 1
A[0,2] = 8 2
i
A[1,0] = 8 0 8 8 8
A[1,1] = 8
1 8 8 8
A[1,2] = 8
A[2,0] = 8 2 8 8 8
A[2,1] = 8 3 8 8 8 4X3
A[2,2] = 3
A[3,0] = 8
A[3,1] = 8
A[3,2] = 8
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
SOLUCIÓN: Para llenar la matriz con el número 8, Se realizará el recorrido habitual por
filas, por lo tanto se necesita un for dentro de otro for , el primero para recorrer todas las
filas, y el segundo que recorra todas las columnas de cada fila. Se realizará el mismo
recorrido para mostrar la matriz.
Para dimensionar una matriz en un Diagrama de Flujo, utilizaremos la palabra DIM seguida
del nombre de la matriz y entre corchetes el número de filas y de columnas de la matriz, es
decir: DIM A[4,3]
EJECUCIÓN
FIN
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
MOSTRAR
CARGAR M
0 1
0 8 3
1 4 2
2 6 1
MEMORIA RAM
DE LA PC
Otra operación que se puede realizar en una matriz, es llenar la misma con elementos
aleatorios
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
mostrar(A[,], f, c)
static void mostrar(int[,] A, int f, int c)
{
int i, j;
i=0, f-1, 1 Console.WriteLine("La matriz es:");
for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
j=0, c-1, 1 {
Console.Write(A[i, j] + "\t");
}
Console.WriteLine();
A[i,j]
}
}
fin
A[I,j]=random()
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
Ejemplo 2.: Cargar una matriz de tamaño mxn, mostrarla y hallar la suma y promedio de
elementos mediante otro procedimiento.
Por ejm. Si m=3 y n=2 y la matriz cargada es:
A
0 1
0 2 3
1 5 4
2 5 1
Mostrar:
La suma de elementos es: 20
El promedio es: 3.33
DISEÑO CODIFICACIÓN
s=s +A[I, j]
p=s/(f*c)
///+A[i][j
s, p
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
using System;
Ejemplo 3: Generar números aleatorios entre 1 y 100 en una matriz de mxn, muestre la
matriz y obtenga la cantidad y suma de números múltiplos de 3 que contenga, por ejm.
0 1 2
0 82 3 15
1 17 21 28
2 6 61 45
3 9 27 11 4X3
multiplos3(A[,], f, c)
static void multiplo3(int[,] A, int f, int c)
{
int d = 0, s = 0, i, j;
d=0 for (i = 0; i < f; i++)
s=0 {
for (j = 0; j < c; j++)
{
i=0, f-1, 1 if (A[i, j] % 3 == 0)
{
d = d + 1;
s = s + A[i, j];
j=0,c-1, 1 }
}
}
Console.WriteLine("Cantidad de múltiplos de
A[i,j]mod3=0 3 es:"+d);
Console.WriteLine("Suma de múltiplos de 3
es:" + s);
d=d+1 }
s=s + A[i,j]
d, s
FIN
PROCEDIMIENTO QUE CARGA NÚMEROS ALEATORIOS EN UNA MATRIZ:
DISEÑO CODIFICACIÓN
aleatorios (A[,],f, c)
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
mostrar(A[,], f, c)
static void mostrar(int[,] A, int f, int c)
{
int i, j;
i=0, f-1, 1 Console.WriteLine("La matriz es:");
for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
j=0, c-1, 1 {
Console.Write(A[i, j] + "\t");
}
Console.WriteLine();
A[i,j]
}
}
fin
PROGRAMA PRINCIPAL
using System;
INICIO namespace Ejempl5Matrices
{
class Program
m,n { // Escribir aquí los procedimientos y funciones
static void Main(string[] args)
{
int m, n;
DIM C[m,n] Console.Write("Número de filas:");
m = int.Parse(Console.ReadLine());
Console.Write("Número de columnas:");
generar(C, m,n) n = int.Parse(Console.ReadLine());
int[,] C = new int[m, n];
mostrar(C,m,n) aleatorios(C, m, n);
multiplos3(C,m,n) mostrar(C, m, n);
multiplo3(C, m, n);
Console.ReadKey();
FIN }
}
}
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
EJECUCIÓN:
Ejemplo 4: Cargue una matriz de mxn, muestre la matriz y muestre y obtenga la cantidad y
de números perfectos que contenga, por ejm.
0 1 2
0 9 6 18
1 13 8 28
2 6 36 3 3X3
cargar(A[,], f, c)
static void cargar(int[,] A, int f, int c)
{
int i, j;
i=0, f-1, 1 Console.WriteLine("Introduzca elementos de la
matriz:");
for (i = 0; i < f; i++)
j=0, c-1,1 {
for (j = 0; j < c; j++)
A[i, j] = int.Parse(Console.ReadLine());
A[i,j] }
}
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
esPerfectos(x) contarPerfectos(A[,], f, c)
s=0
d=0
i=1, x-1, 1
i=0, f-1, 1
xmodi = 0 j=0,c-1, 1
s=s + i p= esPerfecto(A[i,j])
p=1
A[i,j]
x=s
d=d+1
return 1
return 0
PROGRAMA PRINCIPAL
using System;
INICIO namespace Ejemplo4Matrices
{
class Program
m,n {
// Escribir aquí los procedimientos y funciones
static void Main(string[] args)
{
DIM P[m][n] int m, n;
Console.Write("Número de filas:");
m = int.Parse(Console.ReadLine());
cargar(P, m,n) Console.Write("Número de columnas:");
n = int.Parse(Console.ReadLine());
mostrar(P,m,n)
int[,] P = new int[m, n];
contarPerfectos(P,m,n) cargar(P, m, n);
mostrar(p, m, n);
contarPerfectos(P, m, n);
FIN Console.ReadKey();
}
}}
EJECUCIÓN
Ejemplo 3: Cargue una matriz de mxn, muestre la matriz y los factoriales de cada elemento
de la matriz utilizando la función factorial:
Por ejm. si m=3, n=2 y la matriz cargada es:
0 1
0 8 0 Mostrar: 40320 1
1 2 5 2 120
2 6 4 720 24
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
SOLUCIÓN:
fin
{ long f=1;
f=1 int i;
f=f*i return f;
return f
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
cargar(A[,], f, c)
static void cargar(int[,] A, int f, int c)
{
int i, j;
i=0, f-1, 1 Console.WriteLine("Introduzca elementos de la
matriz:");
for (i = 0; i < f; i++)
j=0, c-1,1 {
for (j = 0; j < c; j++)
A[i, j] = int.Parse(Console.ReadLine());
A[i,j] }
}
fin
mostrar(A[,], f, c)
static void mostrar(int[,] A, int f, int c)
{
int i, j;
i=0, f-1, 1 Console.WriteLine("La matriz es:");
for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
j=0, c-1, 1 {
Console.Write(A[i, j] + "\t");
}
Console.WriteLine();
A[i,j]
}
}
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
using System;
INICIO namespace Ejemplo6Matrices
{
class Program
m,n { // Escribir aquí los procedimientos y funciones
static void Main(string[] args)
{
int m, n;
DIM B[m,n] Console.Write("Número de filas:");
m = int.Parse(Console.ReadLine());
Console.Write("Número de columnas:");
cargar(B, m,n) n = int.Parse(Console.ReadLine());
mostrar(B,m,n) int[,] B = new int[m, n];
cargar(B, m, n);
mostrarFactoriales(B,m,n) mostrar(B, m, n);
mostrarFactoriales(B,m,n);
Console.ReadKey();
FIN } }
}
EJECUCIÓN:
0 1 2 0 1 2
0 9 6 1 2 2 7
1 7 8 10 4 1 4
2 6 9 3 3X3 8 9 6 3x3
Entonces la suma es:
z 0 1 2
0 11 12 8
1 11 9 14
2 14 18 9 3X3
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
A[I,j]=random()
fin
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
using System;
INICIO namespace Ejemplo7Matrices
{
class Program
m,n {
// Escribir aquí los procedimientos y funciones
static void Main(string[] args)
{
DIM A[m,n] int m, n;
DIM B[m,n] Console.Write("Número de filas:");
DIM C[m,n] m = int.Parse(Console.ReadLine());
Console.Write("Número de columnas:");
n = int.Parse(Console.ReadLine());
aleatorios(A, m, n) int[,] A = new int[m, n];
int[,] B = new int[m, n];
aleatorios(B, m, n)
int[,] C = new int[m, n];
sumaMatrices(A, B, C, m, n)
aleatorios(A, m, n);
mostrar(A,m,n) aleatorios(B, m, n);
mostrar(B,m,n) sumaMatrices(A, B, C, m, n);
mostrar(C,m,n) Console.WriteLine("La primera matriz es:");
mostrar(A, m, n);
Console.WriteLine("La segunda matriz es:");
FIN mostrar(B, m, n);
Console.WriteLine("La suma de las matrices es:");
mostrar(C, m, n);
Console.ReadKey();
}
}
EJECUCIÓN:
5. Cargue una matriz de mxn con números aleatorios entre 1 y 20, muestre la matriz y
halle la suma de elementos de cada columna mediante un procedimiento. Por ejm.
si la matriz es :
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
7 22 6 3
13 0 2 1
2 4 9 15
sumarColumnas(A[,], f, c)
i=0, c-1, 1
static void sumarColumnas(int[,] A, int f, int c)
{
int i, j, s;
s=0 Console.WriteLine("La suma de
columnas es:");
for (i = 0; i < c; i++)
j=0,f-1, 1 {
s = 0;
for (j = 0; j < f; j++)
s= s+A[j, i] {
s = s + A[j, i];
}
Console.Write(s+"\t");
}
s }
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
static void Main(string[] args)
INICIO {
int m, n;
Console.Write("Número de filas:");
m,n m = int.Parse(Console.ReadLine());
Console.Write("Número de columnas:");
n = int.Parse(Console.ReadLine());
int[,] A = new int[m, n];
DIM A[m, n] aleatorios(A, m,n);
mostrar(A, m, n);
sumarColumnas(A, m, n);
Console.ReadKey();
aleatorios(A, m,n)
}
mostrar(A,m,n)
sumarColumnas(A,m,n)
FIN
generarImpares(A[][], f, c) mostrar(A[,], f, c)
i=0, f-1, 1
j=0, c-1, 1
j=c-1, 0, -1
A[i,j]
A[i][j]=d
d=d+2
fin
fin
PROGRAMA PRINCIPAL
FIN
EJECUCIÓN:
0 1 2 3
0 1 10 11 20
1 2 9 12 19
2 3 8 13 18
3 4 7 14 17
4 5 6 15 16 5X4
Para generar la matriz gusanito realizaremos un recorrido por columnas. Luego para cada columna
las filas se recorren y llenan de la siguiente forma:
• De arriba hacia abajo (de 0 a f-1), si la columna es par
• De abajo hacia arriba (de f-1 a 0), si la columna es impar
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
SOLUCIÓN:
PROCEDIMIENTO QUE GENERA LA MATRIZ GUSANITO
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
FIN
EJECUCIÓN:
0 1 2
0 1 1 2
1 0 0 3
2 0 0 5
3 0 0 8
4 0 0 13
5 0 0 21 6x3
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
matrizLinv(A[], f, c)
PROCEDIMIENTO QUE MUESTRA UNA MATRIZ
mostrar(A[,], f, c)
x=1
y=0
i=0, f-1, 1
i=0,c-1, 1
j=0, c-1, 1
z=x+y
A[0,i]=z
x=y A[i,j]
y=z
i=1, f-1, 1
fin
z=x+y
A[i,c-1]=z static void mostrar(int[,] A, int f, int c)
x=y {
y=z int i, j;
for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
{
Console.Write(A[i, j] + "\t");
}
fin Console.WriteLine();
}
}
PROGRAMA PRINCIPAL
static void Main(string[] args)
INICIO {int m, n;
Console.Write("Número de filas:");
m = int.Parse(Console.ReadLine());
Console.Write("Número de columnas:");
m,n n = int.Parse(Console.ReadLine());
int[,] L = new int[m, n];
matrizLinv(L, m, n);
mostrar(L, m, n);
DIM L[m,n] Console.ReadKey();
}
matrizLinv(L, m,n)
mostrar(L,m,n)
EJECUCIÓN:
FIN
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1 5x5
fin
PROGRAMA PRINCIPAL:
matrizX(X,n) EJECUCIÓN:
mostrar(X,n,n)
FIN
0 1 2 3 4
0 5 4 3 2 1
1 0 6 0 3 0
2 0 0 7 0 0
3 0 0 0 8 0
4 13 12 11 10 9
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
j=n-2,0, -1
A[n-1][j] = c
c=c+1
fin
FIN
EJECUCIÓN:
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
}
j=i, n-1, 1
A[i,j]=c
c=c+3
fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I
PROGRAMA PRINCIPAL
triangularSuperior(T,n)
mostrar(T,n,n)
FIN
EJECUCIÓN: