0% found this document useful (0 votes)
35 views13 pages

Practica Matrices 2

The document contains code for generating and displaying different types of matrices. It is divided into two exercises. The first exercise contains code to generate and display four types of square matrices (A, B, C, D). The second exercise contains code to generate and display four types of matrices with a variable number of rows and columns (2A, 2B, 2C, 2D). For each type of matrix, there are methods to generate the values and display the matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views13 pages

Practica Matrices 2

The document contains code for generating and displaying different types of matrices. It is divided into two exercises. The first exercise contains code to generate and display four types of square matrices (A, B, C, D). The second exercise contains code to generate and display four types of matrices with a variable number of rows and columns (2A, 2B, 2C, 2D). For each type of matrix, there are methods to generate the values and display the matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

…………………… NOTA IMPORTAR LA

LIBRERIA SCANNER EN TODOS LOS


EJERCICIOS ………………………………
Ejercicio 1
Inciso a)
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Ingrese el tamaño de la matriz cuadratica");

int n = sc.nextInt();

int a[][] = new int [n][n];

GenerarA(a,n);

Mostrar(a,n);

public static void GenerarA(int a[][], int n){

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if(i==j || (i+j)==n-1){

a[i][j]=1;

else{

a[i][j] = 0;

public static void Mostrar(int a[][], int n){

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

System.out.print(/*"["+*/a[i][j]/*+"] "*/);

System.out.println("");
}

}
Inciso B)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese el tamaño de la matriz cuadratica");
int n = sc.nextInt();
int a[][] = new int [n][n];
GenerarB(a,n);
Mostrar(a,n);
}

public static void GenerarB(int a[][], int n){


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(j<=i){
a[i][j]=1;
}
else{
a[i][j] = 0;
}
}
}
}
public static void Mostrar(int a[][], int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print("["+a[i][j]+"] ");
}
System.out.println("");
}
}
Inciso C)
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {


System.out.println("Ingrese el tamaño de la matriz");
int n = sc.nextInt();
int a[][] = new int [n][n];
GenerarC(a,n);
Mostrar(a,n);
}

public static void GenerarC(int a[][], int n){


int y = n-1;
int x = 1;
for (int i = 0; i < (n+1)/2; i++) {
for (int j = 0; j < n; j++) {
if(i<=j && (i+j)<n){
a[j][i] = x;
a[j][y] = x;
}
else{
a[j][i]=0;
a[j][y]=0;
}
}
y--;
}
}
public static void Mostrar(int a[][], int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j]);
}
System.out.println("");
}
}
INCISO D)
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {


System.out.println("Ingrese el tamaño de la matriz");
int n = sc.nextInt();
int a[][] = new int [n][n];
GenerarD(a,n);
Mostrar(a,n);
}

public static void GenerarD(int a[][], int n){


int y = n-1;
int x = 1;
for (int i = 0; i < (n+1)/2; i++) {
for (int j = 0; j < n; j++) {
if(i<=j && (i+j)<n){
a[i][j] = x;
a[y][j] = x;
}
else{
a[i][j]=0;
a[y][j]=0;
}
}
y--;
}
}
public static void Mostrar(int a[][], int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j]);
}
System.out.println("");
}
}
EJERCICIO 2
INCISO A)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese la cantidad de filas");
int n = sc.nextInt();
System.out.println("Ingrese la cantidad de columnas");
int m =sc.nextInt();
int a[][] = new int [n][n];
Generar2A(a,n,m);
Mostrar(a,n,m);
}

public static void Generar2A(int a[][], int n, int m){


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j]=i+1;
}
}
}

public static void Mostrar(int a[][], int n, int m){


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(/*"["+*/a[i][j]/*+"] "*/);
}
System.out.println("");
}
}
INCISO B)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese la cantidad de filas");
int n = sc.nextInt();
System.out.println("Ingrese la cantidad de columnas");
int m =sc.nextInt();
int a[][] = new int [n][n];
Generar2B(a,n,m);
Mostrar(a,n,m);
}

public static void Generar2B(int a[][], int n, int m){


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[j][i]=(i+1)*2;
}
}
}

public static void Mostrar(int a[][], int n, int m){


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(/*"["+*/a[i][j]/*+"] "*/);
}
System.out.println("");
}
}
INCISO C)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese la cantidad de filas");
int n = sc.nextInt();
System.out.println("Ingrese la cantidad de columnas");
int m =sc.nextInt();
int a[][] = new int [n][n];
Generar2C(a,n,m);
Mostrar(a,n,m);
}

public static void Generar2C(int a[][], int n, int m){


int x = 1;
for (int i = n-1; i >= 0; i--) {
if(i%2==0){
for (int j = m-1; j >= 0; j--) {
a[i][j] = x;
x++;
}
}
else{
for (int j = 0; j<m ; j++) {
a[i][j] = x;
x++;
}
}
}
}
public static void Mostrar(int a[][], int n, int m){
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print("["+a[i][j]+"]\t");
}
System.out.println("");
}
}
INCISO D)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese la cantidad de filas");
int n = sc.nextInt();
System.out.println("Ingrese la cantidad de columnas");
int m =sc.nextInt();
int a[][] = new int [n][n];
Generar2D(a,n,m);
Mostrar(a,n,m);
}

public static void Generar2D(int a[][], int n, int m){


int x = 1;
for (int i = m-1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
a[j][i] = x;
x++;
}
}
}

public static void Mostrar(int a[][], int n, int m){


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print("["+a[i][j]+"]\t");
}
System.out.println("");
}
}

You might also like