Está en la página 1de 4

1

PROGRAMACIÓN II

EJEMPLOS DE GENERACIÓN DE MATRICES

Ejemplo 2: 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

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

x=1 { int i,x=1, y=0, z;


y=0 for(i=0;i<c;i++)
{ z=x+y;
i=0,c-1, 1 A[0][i]=z;
x=y; y=z;
z=x+y }
A[0][i]=z
x=y for(i=1;i<f;i++)
y=z { z=x+y;
A[i][c-1]=z;
x=y; y=z;
}
i=1, f-1, 1
}

z=x+y
A[i][c-1]=z
x=y
y=z

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");
}
A[i][j] System.out.println();
}
}

fin

PROGRAMA PRINCIPAL

import java.util.Scanner;

INICIO public class Ejemplo2GeneracionMatrices {

m,n //Escribir aquí los procedimientos


public static void main(String[] args) {

DIM L[m][n] int m, n;


Scanner leer=new Scanner(System.in);
System.out.print("Número de filas: ");
Matriz2(L, m,n) m=leer.nextInt();
mostrar(L,m,n) System.out.print("Número de columnas: ");
n=leer.nextInt();
int L[][]= new int[m][n];
matriz2(L, m,n);
FIN System.out.println("La matriz es:");
mostrar(L,m,n);
}
3

EJECUCIÓN:

Ejemplo 3: 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

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

{ int i,j, d=1;


d=1
for(i=0;i<f;i++)

{
i=0, f-1, 1
for(j=c-1;j>=0;j--)

{ A[i][j]=d;
j=c-1, 0, -1
d=d+2;

A[i][j]=d }
d=d+2 }

fin
4

PROGRAMA PRINCIPAL

import java.util.Scanner;
INICIO public class Ejemplo3GeneracionMatrices {
//Escribir aquí el código de los procedimientos

m,n public static void main(String[] args) {

int m, n;
Scanner leer=new Scanner(System.in);
DIM B[m][n]
System.out.print("Número de filas: ");
m=leer.nextInt();
System.out.print("Número de columnas: ");
Matriz3(B, m,n) n=leer.nextInt();
mostrar(B,m,n) int B[][]= new int[m][n];
matriz3(B, m,n);
System.out.println("La matriz es:");
FIN mostrar(B,m,n);
}
}

EJECUCIÓN:

También podría gustarte