Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package test;
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author eliseo
- */
- public class Test {
- public static int dimension;
- public static int[][] matriz1;
- public static int[][] matriz2;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //Imprimir();
- //sumaDeMatrices();
- MultiplicacionDeMatrices();
- }
- public static void Imprimir() {
- int arreglo[] = new int[10000]; // el entero arreglo, tendrá diez mil casillas.
- for (int i = 0; i < 10000; i++) { // Se declara la variabe i en 0 ; mientras i sea menor a diez mil ; aumento de i en 1
- arreglo[i] = (i + 1); // arreglo[] posee diez mil casillas, se le asigna i, como pate del total de las casillas. Como [i] parte en 0, se le suma 1 para partir desde el 1 y no e 0.
- System.out.println(arreglo[i]);
- }
- }
- public static void sumaDeMatrices() {
- int matriz1[][] = new int[2][2];
- int matriz2[][] = new int[2][2];
- int matriz3[][] = new int[2][2];
- matriz1[0][0] = 1;
- matriz1[0][1] = 2;
- matriz1[1][0] = 3;
- matriz1[1][1] = 4;
- matriz2[0][0] = 1;
- matriz2[0][1] = 0;
- matriz2[1][0] = 0;
- matriz2[1][1] = 1;
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- for (int k = 0; k < 1; k++) {
- matriz3[i][j] = (matriz1[i][j] + matriz2[i][j]);
- System.out.print(" " + matriz3[i][j]); //Imprime matriz, en orden de fila y columnas
- }
- }
- System.out.println(""); //Salto de espacio
- }
- }
- public static void MultiplicacionDeMatrices() {
- PreguntarDimension();
- //generar matrices de manera random
- matriz1 = GenerarMatriz();
- matriz2 = GenerarMatriz();
- //mostrar las marices generadas
- System.out.println("Matriz 1:");
- ImprimirMatriz(matriz1);
- System.out.println("Matriz 2:");
- ImprimirMatriz(matriz2);
- System.out.println("Multiplicacion:");
- //multiplicar las matrices
- int[][] matriz_resultado = MultiplicarMatrices(matriz1, matriz2);
- //mostrar el resultado
- ImprimirMatriz(matriz_resultado);
- }
- private static void PreguntarDimension() {
- Scanner en = new Scanner(System.in);
- System.out.print("Ingrese dimensión: ");
- dimension = en.nextInt();
- System.out.println("");
- }
- private static int[][] GenerarMatriz() {
- int[][] m = new int[dimension][dimension];
- for (int i = 0; i < dimension; i++) {
- for (int j = 0; j < dimension; j++) {
- m[i][j] = randInt(0, 9);
- }
- }
- return m;
- }
- //http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java
- public static int randInt(int min, int max) {
- // NOTE: Usually this should be a field rather than a method
- // variable so that it is not re-seeded every call.
- Random rand = new Random();
- // nextInt is normally exclusive of the top value,
- // so add 1 to make it inclusive
- int randomNum = rand.nextInt((max - min) + 1) + min;
- return randomNum;
- }
- private static int[][] MultiplicarMatrices(int[][] matriz1, int[][] matriz2) {
- int[][] r = new int[dimension][dimension];
- //columnas
- for (int i = 0; i < dimension; i++) {
- for (int j = 0; j < dimension; j++) {
- int suma = 0;
- for (int k = 0; k < dimension; k++) {
- suma += matriz1[j][k] * matriz2[k][i];
- }
- r[j][i] = suma;
- }
- }
- return r;
- }
- private static void ImprimirMatriz(int[][] matriz) {
- for (int i = 0; i < dimension; i++) {
- System.out.print("[");
- for (int j = 0; j < dimension; j++) {
- System.out.print(matriz[i][j] + "\t");
- }
- System.out.println("]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement