Está en la página 1de 30

LIC.

REINA BUSTAMANTE PACO PROGRAMACIÓN I

INSTITUTO TECNOLÓGICO
“MARCELO QUIROGA SANTA CRUZ”

CARRERA DE SISTEMAS INFORMATICOS

TEXTO BASE DE PROGRAMACIÓN I

CUARTA ETAPA

CAPÍTULO VI: ARREGLOS BIDIMENSIONALES


LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

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

REFERENCIA A UN ELEMENTO DE UNA MATRIZ:


Para referenciar un elemento de una matriz, se utiliza el nombre de la matriz, el índice de
fila y el índice de columna en el que se encuentra.
Cada posición de una matriz se identifica por 2 índices, el primero corresponde a la fila y el
segundo a la columna:
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

A 0 1 2

0 [0,0] [0,1] [0,2]


1 [1,0] [1,1] [1,2] A[2,1]
2 [2,0] [2,1] [2,2] Índice índice
3 [3,0] [3,1]
[3,2] 4 de fila de columna

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]

DISEÑO DE LA SOLUCIÓN EN CODIFICACIÓN:


DIAGRAMA DE FLUJO:
using System;
INICIO
namespace Ejemplo1Matrices
{
class Program
DIM A[4,3] {
static void Main(string[] args)
i=0, 3, 1 {
int i, j;
int[,] A = new int[4, 3];
j=0, 2, 1 for (i = 0; i <=3 ; i++)
{
for (j = 0; j <=2; j++)
A[i,j] =8 A[i, j] = 8;
}
Console.WriteLine("La matriz es:");
for (i = 0; i<=3; i++)
{
for (j = 0; j <=2; j++)
{
i=0, 3, 1 Console.Write(A[i, j] + "\t");
}
Console.WriteLine();
j=0, 2, 1 }
Console.ReadKey();
}
A[i,j] }
}

EJECUCIÓN
FIN
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

OPERACIONES CON MATRICES:


Se tienen 2 operaciones básicas con matrices:
1. Cargar una matriz: consiste en leer elementos en una matriz
2. Mostrar una matriz: Consiste en mostrar los elementos de una matriz

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

PROCEDIMIENTO QUE CARGA DE ELEMENTOS UNA MATRIZ:


DISEÑO CODIFICACIÓN

cargar(A[,], f, c) static void cargar(int[,] A, int f, int c)


{
int i, j;
Console.WriteLine("Introduzca elementos de la
i=0, f-1, 1 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

PROCEDIMIENTO QUE MUESTRA UNA MATRIZ


DISEÑO CODIFICACIÓN

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

PROCEDIMIENTO QUE CARGA NÚMEROS ALEATORIOS EN UNA MATRIZ:


DISEÑO CODIFICACIÓN

generar (A[,],f, c) static void aleatorios(int[,] A, int f, int c)


{
int i, j;
Random r = new Random();
i=0, f-1, 1 for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
A[i, j] = r.Next(1, 21);
j=0,c-1, 1
}
}

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

SOLUCIÓN: Procedimiento que halla la suma y promedio de una matriz:

DISEÑO CODIFICACIÓN

sumaPromedio(A[,],f, c) static void sumaPromedio(int[,] A, int f, int c)


{
int s = 0, i, j;
float p;
s=0 for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
i=0, f-1, 1 s = s + A[i,j];
}
p = (float)s / (f * c);
Console.WriteLine("La suma es:" + s);
j=0,c-1, 1 Console.WriteLine("El promedio es:" + p);
}

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;

INICIO namespace Ejemplo2Matrices


{
class Program
{
m,n // Incluir aquí los procedimientos anteriores

static void Main(string[] args)


DIM A[m,n] {
int f, c;
Console.Write("Número de filas:");
f = int.Parse(Console.ReadLine());
cargar(A, m,n) Console.Write("Número de columnas:");
mostrar(A,m,n) c = int.Parse(Console.ReadLine());
sumaPromedio(A,m,n) int[,] A = new int[f, c];
cargar(A, f, c);
mostrar(A, f, c);
sumaPromedio(A,f,c);
FIN Console.ReadKey();
}
}
}

EJECUCIÓN DEL PROGRAMA:

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

Cantidad de múltiplos de tres: 7


Suma de múltiplos de tres: 126
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

SOLUCIÓN: PROCEDIMIENTO QUE HALLA Y MUESTRA LA CANTIDAD Y SUMA DE LOS


ELEMENTOS MÚLTIPLOS DE 3 DE UNA MATRIZ

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)

static void aleatorios(int[,] A, int f, int c)


{
i=0, f-1, 1 int i, j;
Random r = new Random();
for (i = 0; i < f; i++)
{
j=0,c-1, 1
for (j = 0; j < c; j++)
A[i, j] = r.Next(1, 101);
}
A[I,j]=random() }

fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

PROCEDIMIENTO QUE MUESTRA UNA MATRIZ


DISEÑO CODIFICACIÓN

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

Números perfectos: 6, 28, 6


Cantidad de números perfectos: 3

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.

PROCEDIMIENTO QUE CARGA DE ELEMENTOS UNA MATRIZ:


DISEÑO CODIFICACIÓN

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

FUNCIÓN QUE VERIFICA SI UN NÚMERO PROCEDIMIENTO QUE MUESTRA Y HALLA LA

ES PERFECTO CANTIDAD DE NÚMEROS PERFECTOS DE UNA


MATRIZ

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

static void contarPerfectos(int[,] A, int f, int c)


d
{
int i, j, p,d = 0;
Console.WriteLine("Los números perfectos son:");
for (i = 0; i < f; i++) FIN
{
for (j = 0; j < c; j++)
{ static int esPerfecto(int x)
p = esPerfecto(A[i,j]); {
if (p == 1)
{ int i, s=0;
Console.WriteLine(A[i,j] + "\t"); for(i=1; i<x; i++)
d =d+1; { if(x%i==0)
}
} s=s+i;
} }
if(x==s)
Console.WriteLine("Cantidad de números
perfectos:" + d);
return 1;
} return 0;
}
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

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:

PROCEDIMIENTO QUE HALLA Y MUESTRA LOS FACTORIALES DE LOS ELEMENTOS LA MATRIZ

mostrarFactoriales(A[,], f, c) static void mostrarFactoriales(int[,] A, int f,


int c)
{
i=0, f-1, 1 int i, j;
long fac;
Console.WriteLine("Los factoriales son:");
for (i = 0; i < f; i++)
j=0, c-1, 1 {
for (j = 0; j < c; j++)
{
fac = factorial(A[i, j]);
fac=factorial(A[I,j] Console.Write(fac + "\t");
) }
Console.WriteLine();
fac }
}

fin

factorial(x) static long factorial(int x )

{ long f=1;
f=1 int i;

for (i = 1; i <= x; i++)


i= 1, x, 1
f = f * i;

f=f*i return f;

return f
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

PROCEDIMIENTO QUE CARGA DE ELEMENTOS UNA MATRIZ:


DISEÑO CODIFICACIÓN

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

PROCEDIMIENTO QUE MUESTRA UNA MATRIZ


DISEÑO CODIFICACIÓN

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:

Ejemplo 4: Generar números aleatorios entre 1 y 10 en 2 matrices de mxn, sume las


matrices y finalmente muestre las 3 matrices por ejm.

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

PROCEDIMIENTO QUE CARGA NÚMEROS ALEATORIOS EN UNA MATRIZ:


DISEÑO CODIFICACIÓN

aleatorios (A[,],f, c) static void aleatorios(int[,] A, int f, int c)


{
int i, j;
Random r = new Random();
i=0, f-1, 1 for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
j=0,c-1, 1 A[i, j] = r.Next(1, 101);
}
}

A[I,j]=random()

fin

PROCEDIMIENTO QUE SUMA DOS MATRICES

sumaMatrices (A[,],B[,], C[,], f, c)

i=0, f-1, 1 static void sumaMatrices(int[,] A, int[,] B,


int[,] C, int f, int c)
{
int i, j;
j=0,c-1, 1 for (i = 0; i < f; i++)
{
for (j = 0; j < c; j++)
C[I,j]=A[I,j] +B[I,j] {
C[i,j]=A[i, j] +B[i,j];
}
}
}

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

Las suma de cada columna es: 22, 26, 17 y 19

SOLUCIÓN: Para resolver el ejercicio se debe realizar un recorrido por columnas,


ya que debemos empezar sumando los elementos de la primera columna, luego de
la segunda columna y así sucesivamente.

Para resolver el ejercicio, tomar en cuenta las siguientes pautas:


Cuando el recorrido es por columnas, el primer for controla columnas y el segundo
for controla filas.
El contador que controla columnas siempre va como segundo índice al referenciar
un elemento de una matriz y el contador que controla filas siempre va como primer
índice.

PROCEDIMIENTO QUE SUMA LOS ELEMENTOS DE CADA COLUMNA DE UNA MATRIZ Y


LOS MUESTRA

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

EJECUCIÓN DEL PROGRAMA:

EJEMPLOS DE GENERACIÓN DE MATRICES


Ejemplo 1: Genere la siguiente matriz de mxn:
0 1 2
0 5 3 1
1 11 9 7
2 17 15 13
3 23 21 19 4X3
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

PROCEDIMIENTO QUE GENERA LA MATRIZ

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

d=1 i=0, f-1, 1

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

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


int f, int c) {
{ int i, j;
int d = 1, i, j; Console.WriteLine("La matriz es:");
for(i=0; i<f; i++) for (i = 0; i < f; i++)
{ for (j = c - 1; j >= 0; j--) {
{ for (j = 0; j < c; j++)
A[i, j] = d; {
d = d + 2; Console.Write(A[i, j] + "\t");
} }
} Console.WriteLine();
} }
}
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

PROGRAMA PRINCIPAL

static void Main(string[] args)


INICIO {
int m, n;
Console.WriteLine("Número de filas:");
m = int.Parse(Console.ReadLine());
m,n Console.WriteLine("Número de columnas:");
n = int.Parse(Console.ReadLine());
int[,] B= new int[m, n];
DIM B[m,n] generarImpares(B, m, n);
mostrar(B,m,n);
Console.ReadKey();
generarImpares(B, m,n) }
mostrar(B,m,n)

FIN

EJECUCIÓN:

Ejemplo 2: Generar la matriz gusanito:

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

gusanito(A[][], f, c) static void gusanito(int[,] A, int f, int c)


{
int d = 1, k, l;
for (k = 0; k < c; k++)
d=1 {
if (k % 2 == 0)
{
k=0; c-1; 1 for (l = 0; l < f; l++)
{
A[l, k] = d;
d = d + 1;
F V }
kmod2==0 }
22d else
{
for (l = f - 1; l >= 0; l--)
l= f-1;0; -1 l=0; f-1; 1 {
A[l, k] = d;
d = d + 1;
}
A[l][k]=d A[l][k]=d }
d=d+1 d=d+1 }
}

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 = int.Parse(Console.ReadLine());
m,n Console.Write("Número de columnas:");
n = int.Parse(Console.ReadLine());
int[,] G = new int[m, n];
gusanito(G, m, n);
DIM G[m][n] mostrar(G, m, n);
Console.ReadKey();
}
gusanito(G, m,n)
mostrar(G,m,n)

FIN

EJECUCIÓN:

Ejemplo 3: Genere la siguiente matriz de mxn:

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

static void matrizLinv(int[,] A, int f, int c)


{
int x = 1, y = 0, i, z;
for (i = 0; i < c; i++)
{
z = x + y;
A[0, i] = z;
x = y; y = z;
}
for (i = 1; i < f; i++)
{
z = x + y;
A[i, c-1] = z;
x = y; y = z;
}
}
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 = 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

Ejemplo 4: Genere la siguiente matriz de nxn:

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

PROCEDIMIENTO QUE GENERA LA MATRIZ X:


LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

matrizX(A[,],n) static void matrizX(int[,] A,int n)


{
int i, j;
i=0,n-1,1
for (i = 0; i < n; i++)
{
j=0,n-1,1
for (j = 0; j < n; j++)
{
if (i==j || i+j==n-1)
A[i, j]=1 ;
i=j or i+j= n-1 }
}
A[i,j]=1 }

fin

PROGRAMA PRINCIPAL:

static void Main(string[] args)


INICIO
{ int n;
Console.Write("Tamaño de la matriz:");
n = int.Parse(Console.ReadLine());
n int[,] X = new int[n, n];
matrizX(X,n);
mostrar(X,n,n);
Console.ReadKey();
DIM X[n,n]
}

matrizX(X,n) EJECUCIÓN:
mostrar(X,n,n)

FIN

Ejemplo 5: Genere la siguiente matriz de nxn:

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

PROCEDIMIENTO QUE GENERA LA MATRIZ:

generaMatrizZ (A[][], n) static void generaMatrizZ(int[,] A, int n)


{
c=1
int i, j, c = 1;
for (j = n - 1; j >= 0; j--)
{
j=n-1,0, -1 A[0,j] = c;
c++;
A[0][j] = c
}
c=c+1 for (i = 1; i <= n - 1; i++)
{
A[i,i] = c;
c++;
}
i=1,n-1, 1
for (j = n - 2; j >= 0; j--)
{
A[i][i] = c A[n - 1, j] = c;
c=c+1 c++;
}
}

j=n-2,0, -1

A[n-1][j] = c
c=c+1

fin

PROGRAMA PRINCIPAL CODIFICACIÓN

INICIO static void Main(string[] args)


{ int n;
Console.Write("Tamaño de la matriz:");
n n = int.Parse(Console.ReadLine());
int[,] Z = new int[n, n];
generaMatrizZ(Z,n);
mostrar(Z,n,n);
DIM Z[n,n]
Console.ReadKey();
}
generaMatrizZ(Z, n)
mostrar(Z,n,n)

FIN

EJECUCIÓN:
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

Ejemplo 6: Genere la siguiente matriz cuadrada de nxn:


0 1 2 3
0 3 6 9 12
1 0 15 18 21
2 0 0 24 27
3 0 0 0 30 4X4

PROCEDIMIENTO QUE GENERA LA MATRIZ


DIAGRAMA DE FLUJO CODIFICACIÓN

static void triangularSuperior(int[,] A, int n)


triangularSuperior(A[,], n) {
int c = 3, i, j;
for (i = 0; i < n; i++)
c=3 {for (j = i; j < n; j++)
{
A[i, j] = c;
c = c + 3;
i=0, n-1, 1 }
}

}
j=i, n-1, 1

A[i,j]=c
c=c+3

fin
LIC. REINA BUSTAMANTE PACO PROGRAMACIÓN I

PROGRAMA PRINCIPAL

static void Main(string[] args)


INICIO {
int n;
Console.Write("Dimensión de la matriz:");
n = int.Parse(Console.ReadLine());
n int[,] T = new int[n, n];
triangularSuperior(T,n);
mostrar(T, n, n);
Console.ReadKey();
DIM T[n,n] }

triangularSuperior(T,n)
mostrar(T,n,n)

FIN

EJECUCIÓN:

También podría gustarte