> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud2-solucions/arrays/arrays-bidimensionales.md).

# Arrays bidimensionales

1. Suma de matrices

```java
public class SumaDuasMatrices {

    static int[][] inicializarMatriz(int filas, int columnas) {
        int[][] matriz = new int[filas][columnas];

        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                matriz[i][j] = (int) (Math.random() * 51); // valores entre 0 e 50
            }
        }
        return matriz;
    }

    static void mostrarMatriz(int[][] matriz) {
        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz[i].length; j++) {
                System.out.printf("%4d", matriz[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    static int[][] sumarMatriz(int[][] a, int[][] b) throws Exception {
        // Comprobar que teñen as mesmas dimensións
        if (a.length != b.length || a[0].length != b[0].length) {
            throw new Exception("Erro: as matrices deben ter as mesmas dimensións.");
        }

        int filas = a.length;
        int columnas = a[0].length;
        int[][] suma = new int[filas][columnas];

        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                suma[i][j] = a[i][j] + b[i][j];
            }
        }

        return suma;
    }

    public static void main(String[] args) {
        try {
            // Crear e inicializar dúas matrices 3x3
            int[][] matrizA = inicializarMatriz(3, 3);
            int[][] matrizB = inicializarMatriz(3, 3);

            System.out.println("Matriz A:");
            mostrarMatriz(matrizA);

            System.out.println("Matriz B:");
            mostrarMatriz(matrizB);

            // Sumar as dúas matrices
            int[][] matrizSuma = sumarMatriz(matrizA, matrizB);

            System.out.println("Matriz Suma (A + B):");
            mostrarMatriz(matrizSuma);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

```

2. Control de stock

```java
public class StockProductos {

    static int[][] inicializarInventario(int productos, int almacenes){
        int[][] inv = new int[productos][almacenes];
        for (int i = 0; i<productos; i++){
            for (int j = 0; j<almacenes; j++){
                inv[i][j] = (int) (Math.random()*101);
            }
        }
        return inv;
    }

    static void mostrarInventario(int[][] inventario){
        String res = "";
        for (int i = 0; i< inventario.length; i++){
            res = "\n Producto (" + i + ") = {";
            for (int j = 0; j< inventario[0].length; j++){
                res += "\n \t almacen [" + j + "]= " + inventario[i][j];
            }
            res += "}";
            System.out.println(res);
        }
    }

    static int[] cantidadProducto(int[][] inventario){
        int[] res = new int[inventario.length];
        for (int i = 0; i<inventario.length; i++){
            res[i] = 0;
            for (int j = 0; j<inventario[0].length; j++){
                res[i] += inventario[i][j];
            }
        }
        return res;
    }

    static void mostrarCantProd(int[] cantProd){
        String res = "";
        for (int i = 0; i< cantProd.length; i++){
            res = "\n Producto (" + i + ") = {" + cantProd[i] +"}";
            System.out.println(res);
        }
    }

    static int[] maiorStock(int[][] inventario){
        int[] coord = new int[2];
        int stockMaior = -1;
        for (int i = 0; i< inventario.length; i++){
            for (int j = 0; j< inventario[0].length; j++){
                if (inventario[i][j]>stockMaior){
                    stockMaior = inventario[i][j];
                    coord[0] = i;
                    coord[1] = j;
                }
            }
        }
        return coord;
    }

    public static void main(String[] args) {
        int[][] inventario = inicializarInventario(3,4);
        mostrarInventario(inventario);
        int [] cantProd = cantidadProducto(inventario);
        mostrarCantProd(cantProd);
        int[] coord = maiorStock(inventario);
        System.out.println("Coordenadas: " + coord[0] + ", " + coord[1]);
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud2-solucions/arrays/arrays-bidimensionales.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
