Você está na página 1de 12

PRACTICA

ARREGLOS BIDIMENCIONALES
MATRICES
NOMBRE.-DAVID CONDORI CHINO
CARRERA.-SISTEMAS INFORMATICOS
DOCENTE.- LIC HILDA CALLIZAYA APAZA

1.llenar la matriz x con n filas y m columnas con datos de tipo cadena “string”,
mostrar en la pantalla.
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
static void llenarmatriz(String M[][],int f,int c)
{
Scanner lee=new Scanner (System.in)
;
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("ingrese dato:");
M[i][j]=lee.next();
}

}
}
static void Mostrar_Matriz(String M[][],int f,int c)
{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
2. llenar la matriz de n x n.
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1

package javaapplication5;
import java.util.Scanner;
public class JavaApplication5 {
static void llenarmatriz(int M[][],int f,int c)
{

int i,j;
for(i=0;i<f;i++)
{

for(j=0;j<c;j++)
{
if(i==0)
M[i][j]=1;
else
if(i!=(f-1))
M[i][j]=0;
else
M[i][j]=1;
}

}
}
static void Mostrar_Matriz(int M[][],int f,int c)
{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
// TODO code application logic here
Scanner lee=new Scanner(System.in);
System.out.print("introdusca cantidad de filas:");
int f=lee.nextInt();
System.out.print("introdusca cantidad de columnas:");
int c=lee.nextInt();
int M[][]=new int [f][c];
llenarmatriz(M,f,c);
Mostrar_Matriz(M,f,c);

}
}
3. llenar la matriz de s x s sabiendo que es número impar.
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

package javaapplication8;
import java.util.Scanner;
public class JavaApplication8{

static void llenar_matriz (int v[][],int f, int c)


{
int i,j;
for (i=0; i <f; i++){
for (j=0;j<c;j++){
if((i==j) || (i+j)==(c-1))
v[i][j]=1;
else
v[i][j]=0;
}
}
}
static void mostrar_matriz(int v[][],int f, int c)
{
int i,j;
for (i=0; i <f; i++){
for (j=0;j<c;j++){
System.out.print("["+v[i][j]+"]");
}
System.out.println(); //permite el salto de linea entre filas
}
System.out.println();//salto entre columnas
}
public static void main(String[] args) {
Scanner lee=new Scanner (System.in);
System.out.print("Introduzca cantidad de filas: ");
int f=lee.nextInt();
System.out.print("Introduzca cantidad de columnas: ");
int c=lee.nextInt();
int v[][]=new int [f][c];
llenar_matriz(v,f,c);
mostrar_matriz(v,f,c);
}
}

4. escribir un programa que lea colores de tamaño n x m y determine cuantas


veces se repite el color azul y cambiarlo por el color gris.

package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
static void llenarmatriz(String M[][],int f,int c)
{
Scanner lee=new Scanner (System.in)
;
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("ingrese dato:");
M[i][j]=lee.next();
}

}
}
static void Mostrar_Matriz(String M[][],int f,int c)
{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
static void deterycambie(String v[][],int n ,int m,String c, String g)
{
int i,j,co=0;

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (v[i][j] == null ? c == null : v[i][j].equals(c))
{
co=co+1;
v[i][j]=g;
}

}
}
System.out.print("el color azul se sepite "+co);
Mostrar_Matriz(v,n,m);
}
public static void main(String[] args) {

Scanner lee=new Scanner(System.in);


System.out.print("introdusca cantidad de filas:");
int f=lee.nextInt();
System.out.print("introdusca cantidad de columnas:");
int c=lee.nextInt();
System.out.print("introdusca color azul:");
String k=lee.next();
System.out.print("introdusca color gris:");
String g=lee.next();
String M[][]=new String [f][c];
llenarmatriz(M,f,c);
Mostrar_Matriz(M,f,c);
deterycambie(M,f,c,k,g);

}
}
5. hallar la transpuesta de una matriz.
ENTRADA
1 2 3 4
5 6 7 8
SALIDA
1 5
2 6
3 7
4 8
package javaapplication7;
import java.util.Scanner;
public class JavaApplication7 {

static void llenar_matriz (int v[][],int n, int m)


{
Scanner lee=new Scanner (System.in);
int i,j;
for (i=0; i <n; i++)
{
for (j=0;j<m;j++)
{
System.out.print("Ingrese dato: ");
v[i][j]=lee.nextInt();
}
}
}
static void mostrar_matriz(int v[][],int n, int m)
{
int i,j;
for (i=0; i <n; i++){
for (j=0;j<m;j++){
System.out.print("["+v[i][j]+"]");
}
System.out.println(); //permite el salto de linea entre filas
}
System.out.println();//salto entre columnas
}
static void transpuesta(int v[][],int n, int m)
{
int x[][]=new int[m][n];
int i,j;
for (i=0; i <n; i++){
for (j=0;j<m;j++){
x[j][i]=v[i][j];
}

}
mostrar_matriz(x,m,n);
}
public static void main(String[] args) {
Scanner lee=new Scanner (System.in);
System.out.print("Introduzca cantidad de filas: ");
int n=lee.nextInt();
System.out.print("Introduzca cantidad de columnas: ");
int m=lee.nextInt();
int v[][]=new int [n][m];
llenar_matriz(v,n,m);
mostrar_matriz(v,n,m);
transpuesta(v,n,m);
}
}

6. llenar con datos enteros las matrices A y B de tamaño n x m. mostrar el


siguiente resultado 2A+B.utilizar solo dos matrices.
package javaapplication2;
import java.util.Scanner;
public class JavaApplication2 {
static void llenarmatriz(int M[][],int f,int c)
{
Scanner lee=new Scanner (System.in)
;

int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("ingrese dato:");
M[i][j]=lee.nextInt();
}

}
}
static void Mostrar_Matriz(int M[][],int f,int c)
{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
static void suma(int M [][],int N[][],int f,int c)
{
int S[][]=new int [f][c];
S[0][0]=0;
int i,j,aux,m=0;

for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{

m=m+M[i][j]+ N[i][j];
S[i][j]=m;
m=0;
}
}
Mostrar_Matriz(S,f,c);
}
static void multiplica (int M [][],int f,int c,int N[][])
{
Scanner lee=new Scanner(System.in);
int x ;
System.out.print("introdusca x:");
x=lee.nextInt();
int i,j,aux;

for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
aux=M[i][j]*x;
M[i][j]=aux;
}

}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner lee=new Scanner(System.in);
System.out.print("introdusca cantidad de filasy columnas");
int f=lee.nextInt();

int A[][]=new int [f][f];


int B[][]=new int [f][f];
llenarmatriz(A,f,f);
Mostrar_Matriz(A,f,f);
llenarmatriz(B,f,f);
Mostrar_Matriz(B,f,f);
multiplica (A,f,f,B);
suma(A,B,f,f);
}
}

7. mostrar la matriz caracol en pantalla.


package javaapplication3;
import java.util.Scanner;
public class JavaApplication3 {

public static int[][] generarMatrizCaracol(int n, int x) {


int[][] M = new int[n + 1][n + 1];
for (int a = 1; a <= n / 2; a++) {
for (int i = a; i <= n - a; i++) {
M[a][i] = x;
x++;
}
for (int i = a; i <= n - a; i++) {
M[i][n - a + 1] = x;
x++;
}
for (int i = n - a + 1; i >= a + 1; i--) {
M[n - a + 1][i] = x;
x++;
}
for (int i = n - a + 1; i >= a + 1; i--) {
M[i][a] = x;
x++;
}
}
if (n % 2 == 1) {
M[n / 2 + 1][n / 2 + 1] = x;
}
return M;

static void Mostrar_Matriz(int M[][],int f,int c)


{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {

Scanner lee=new Scanner(System.in);


System.out.print("introdusca cantidad de filasy columnas");
int f=lee.nextInt();
System.out.print("introdusca x:");
int x=lee.nextInt();
int A[][]=new int [f][f];
int M[][]=new int [f][f];
M=generarMatrizCaracol(f,x);
Mostrar_Matriz(M,f,f);

}
}
8. realizar la suma de dos matrices de n x m, ambas matrices
package javaapplication4;
import java.util.Scanner;
public class JavaApplication4 {
static void llenarmatriz(int M[][],int f,int c)
{
Scanner lee=new Scanner (System.in)
;

int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("ingrese dato:");
M[i][j]=lee.nextInt();
}

}
}
static void Mostrar_Matriz(int M[][],int f,int c)
{
int i,j;
for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{
System.out.print("["+M[i][j]+"]");

}
System.out.println();
}
System.out.println();
}
static void suma(int M [][],int N[][],int f,int c)
{
int S[][]=new int [f][c];
S[0][0]=0;
int i,j,aux,m=0;

for(i=0;i<f;i++)
{
for(j=0;j<c;j++)
{

m=m+M[i][j]+ N[i][j];
S[i][j]=m;
m=0;
}
}
Mostrar_Matriz(S,f,c);
}
public static void main(String[] args) {
// TODO code application logic here
Scanner lee=new Scanner(System.in);
System.out.print("introdusca cantidad de filasy columnas");
int f=lee.nextInt();

int A[][]=new int [f][f];


int B[][]=new int [f][f];
llenarmatriz(A,f,f);
Mostrar_Matriz(A,f,f);
llenarmatriz(B,f,f);
Mostrar_Matriz(B,f,f);
suma(A,B,f,f);
}
}
Caracol---------------------
public static int[][] generarMatrizCaracol(int n, int x) {
int[][] M = new int[n + 1][n + 1];
for (int a = 1; a <= n / 2; a++) {
for (int i = a; i <= n - a; i++) {
M[a][i] = x;
x++;
}
for (int i = a; i <= n - a; i++) {
M[i][n - a + 1] = x;
x++;
}
for (int i = n - a + 1; i >= a + 1; i--) {
M[n - a + 1][i] = x;
x++;
}
for (int i = n - a + 1; i >= a + 1; i--) {
M[i][a] = x;
x++;
}
}
if (n % 2 == 1) {
M[n / 2 + 1][n / 2 + 1] = x;
}
return M;
}

Você também pode gostar