gggg

miércoles, 3 de abril de 2013

OPERACIONES CON MATRICES EN JAVA


Código Fuente

class operaciones_matrices_completa
{    static void mostrar (int a [] [], int n, int m)
    {  int i, j;
        for (i = 1 ; i <= m ; i++)
        {
            for (j = 1 ; j <= n ; j++)
                System.out.print (a [i] [j] + " ");
            System.out.println ();
        }
    }
    static void leer (int a [] [], int n, int m)
    {  int i, j;
        for (i = 1 ; i <= m ; i++)
        {
            for (j = 1 ; j <= n ; j++)
                a [i] [j] = Leer.datoInt ();
        }
    }
    static void suma (int a [] [], int b [] [], int n, int m)
    {
        int i, j, sum = 0;
        for (i = 1 ; i <= m ; i++)
        {
            for (j = 1 ; j <= n ; j++)
            {
                sum = a [i] [j] + b [i] [j];
                System.out.print (sum + " ");
            }
            System.out.println ();
        }
    }
    static void resta (int a [] [], int b [] [], int n, int m)
    {
        int i, j, res = 0;
        for (i = 1 ; i <= m ; i++)
        {
            for (j = 1 ; j <= n ; j++)
            {
                res = a [i] [j] - b [i] [j];
                System.out.print (res + " ");
            }
            System.out.println ();
        }
    }  static void multi (int a [] [], int b [] [], int c [] [], int n, int m)
    { int i, j, mul = 1, sum = 0;
        for (i = 1 ; i <= m ; i++)
        {
            for (j = 1 ; j <= n ; j++)
            {
                c [i] [j] = 0;
                for (int k = 1 ; k <= n ; k++)
                {
           c [i] [j] += a [i] [k] * b [k] [j];
                }
            }
        }
    }
    public static void main (String [] args)
    {   int i, j, k, l, m = 0, n = 0, opc;
        int a [] [] = new int [50] [50];
        int b [] [] = new int [50] [50];
        int c [] [] = new int [50] [50];
        int u [] [] = new int [50] [50];
        int inv [] [] = new int [50] [50];
      do{
            System.out.println ("Opciones ");
            System.out.println ("1.- Introducir matrices A  y B ");
            System.out.println ("2.- A + B  ");
            System.out.println ("3.- A - B  ");
            System.out.println ("4.- A * B  ");
         System.out.println ("0.- SALIR  ");
            System.out.println ("Elige una opcion ");
            opc = Leer.datoInt ();
            switch (opc)
            {
                case 1:
                    System.out.println ("Introduzca el valor de n :");
                    n = Leer.datoInt ();
                    System.out.println ("Introduzca el valor de m :");
                    m = Leer.datoInt ();
                    System.out.println ("Introduciendo los valores de la matriz A");
                    leer (a, n, m);
                    System.out.println ("Introduciendo los valores de la matriz B");
                    leer (b, n, m);
                    System.out.println ("matriz A");
                    mostrar (a, n, m);
                    System.out.println ("matriz B");
                    mostrar (b, n, m);
                    break;
                case 2:
                    System.out.println ("Matriz A ");
                    mostrar (a, n, m);
                    System.out.println ("Matriz B ");
                    mostrar (b, n, m);
                    System.out.println ("Suma del matriz A+B");
                    suma (a, b, n, m);
                    break;
                case 3:
                    System.out.println ("Matriz A ");
                    mostrar (a, n, m);
                    System.out.println ("Matriz B ");
                    mostrar (b, n, m);
                    System.out.println ("Resta del matriz A-B");
                    resta (a, b, n, m);
                    break;
                case 4:
                    System.out.println ("Matriz A ");
                    mostrar (a, n, m);
                    System.out.println ("Matriz B ");
                    mostrar (b, n, m);
                    System.out.println ("Producto del matriz AxB");
                    multi (a, b, c, n, m);
                    mostrar (c, n, m);
                    break;

            }
        }
        while (opc != 0);
    }
}



7 comentarios:

  1. package transpuesta;

    import java.util.*;

    public class Transpuesta {
    public static void imprimir(int matriz[][], int fila, int columna)
    {
    int i, j;
    for (i=0; i<fila; i++)
    {
    for (j=0; j<columna; j++)
    {
    System .out.print(matriz[i][j]+"\t");

    }
    System .out.println(" ");
    }
    System.out.println();
    }


    public static void main(String[] args) {
    int i,j,fil,colum;
    fil = ((int)(Math.random()*(10-2+1)+2)); //(int)(Math.random()*(HASTA-DESDE+1)+DESDE);
    colum = ((int)(Math.random()*(10-2+1)+2));
    int [] [] matriz = new int [fil][colum];
    int [] [] matriz_T = new int [colum][fil];

    System.out.println("La matriz es: ");

    Random num = new Random();

    for ( i = 0; i < fil; i++) {
    for (j = 0; j < colum; j++) {
    matriz [i][j] = matriz [i][j]=((int)(Math.random()*(100-2+1)+2));
    }
    }
    imprimir (matriz,fil,colum);

    //transpuesta
    System.out.println("La transpuesta de la matriz es: ");
    for(j=0;j<colum;j++)
    {
    for (i=0;i<fil;i++)
    {
    matriz_T[j][i]=matriz[i][j];

    }
    }
    imprimir (matriz_T,colum,fil);
    }

    }

    ResponderBorrar
  2. Este comentario ha sido eliminado por el autor.

    ResponderBorrar
  3. //Universidad Central del Ecuador
    //Franklin Ushiña
    //Examen Programacion
    //Paralelo 9
    //Ing. Lucero Wagner


    //Primer paso: LLamamos a las librerias quevamos a utilizar
    package progra.examen;
    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class PrograExamen
    {
    public static int dimension()
    {
    int a;

    a=Integer.parseInt(JOptionPane.showInputDialog("Ingresa la dimenmsion de la matriz cuadrada"));


    return a;

    }
    public static void llenar_matriz()
    {
    int i,j, m;
    m=dimension();
    int[][] matriz=new int[m][m];
    for (i=0;imax)
    {
    max=matriz[i][j];
    x=x-1;
    }
    }
    }

    }
    System.out.println("El maximo de la segunda diagonal de su matriz es: "+max);

    }



    public static void main(String[] args)
    {
    llenar_matriz();

    }
    }

    ResponderBorrar
  4. package transpuesta;

    import java.util.*;

    public class Transpuesta {
    public static void imprimir(int matriz[][], int fila, int columna)
    {
    int i, j;
    for (i=0; i<fila; i++)
    {
    for (j=0; j<columna; j++)
    {
    System .out.print(matriz[i][j]+"\t");

    }
    System .out.println(" ");
    }
    System.out.println();
    }


    public static void main(String[] args) {
    int i,j,fil,colum;
    fil = ((int)(Math.random()*(10-2+1)+2)); //(int)(Math.random()*(HASTA-DESDE+1)+DESDE);
    colum = ((int)(Math.random()*(10-2+1)+2));
    int [] [] matriz = new int [fil][colum];
    int [] [] matriz_T = new int [colum][fil];

    System.out.println("La matriz es: ");

    Random num = new Random();

    for ( i = 0; i < fil; i++) {
    for (j = 0; j < colum; j++) {
    matriz [i][j] = matriz [i][j]=((int)(Math.random()*(100-2+1)+2));
    }
    }
    imprimir (matriz,fil,colum);

    //transpuesta
    System.out.println("La transpuesta de la matriz es: ");
    for(j=0;j<colum;j++)
    {
    for (i=0;i<fil;i++)
    {
    matriz_T[j][i]=matriz[i][j];

    }
    }
    imprimir (matriz_T,colum,fil);
    }

    }

    ResponderBorrar
  5. cual seria la linea de cidigo en html, para mandar a llamar el codigo del js??

    ResponderBorrar