2

Tengo un ejercicio en el que a partir de dos vectores cree una matriz en el que vector1[i]*Vector2[j] sea igual a nuestra matriz[i][j] ( en este caso me piden que el vector 1 sea de 50 y el vector2 sea de 20, la matriz seria [50][20]

Tengo que realizar algo como esto: ( en el que se vean los vectores 1 y 2 en horizontal y vertical de mi matriz y en su interior se vayan multiplicando las posiciones).

---------------
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,
3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,
4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,
5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,
6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,
7,14,21,28,35,42,49,56,63,70,77,84,91,98,105,112,119,126,133,140,
8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128,136,144,152,160,
9,18,27,36,45,54,63,72,81,90,99,108,117,126,135,144,153,162,171,180,
10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,
11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187,198,209,220,
12,24,36,48,60,72,84,96,108,120,132,144,156,168,180,192,204,216,228,240,
13,26,39,52,65,78,91,104,117,130,143,156,169,182,195,208,221,234,247,260,
14,28,42,56,70,84,98,112,126,140,154,168,182,196,210,224,238,252,266,280,
15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,
16,32,48,64,80,96,112,128,144,160,176,192,208,224,240,256,272,288,304,320,
17,34,51,68,85,102,119,136,153,170,187,204,221,238,255,272,289,306,323,340,
18,36,54,72,90,108,126,144,162,180,198,216,234,252,270,288,306,324,342,360,
19,38,57,76,95,114,133,152,171,190,209,228,247,266,285,304,323,342,361,380,
20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,
21,42,63,84,105,126,147,168,189,210,231,252,273,294,315,336,357,378,399,420,
22,44,66,88,110,132,154,176,198,220,242,264,286,308,330,352,374,396,418,440,
23,46,69,92,115,138,161,184,207,230,253,276,299,322,345,368,391,414,437,460,
24,48,72,96,120,144,168,192,216,240,264,288,312,336,360,384,408,432,456,480,
25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,
26,52,78,104,130,156,182,208,234,260,286,312,338,364,390,416,442,468,494,520,
27,54,81,108,135,162,189,216,243,270,297,324,351,378,405,432,459,486,513,540,
28,56,84,112,140,168,196,224,252,280,308,336,364,392,420,448,476,504,532,560,
29,58,87,116,145,174,203,232,261,290,319,348,377,406,435,464,493,522,551,580,
30,60,90,120,150,180,210,240,270,300,330,360,390,420,450,480,510,540,570,600,
31,62,93,124,155,186,217,248,279,310,341,372,403,434,465,496,527,558,589,620,
32,64,96,128,160,192,224,256,288,320,352,384,416,448,480,512,544,576,608,640,
33,66,99,132,165,198,231,264,297,330,363,396,429,462,495,528,561,594,627,660,
34,68,102,136,170,204,238,272,306,340,374,408,442,476,510,544,578,612,646,680,
35,70,105,140,175,210,245,280,315,350,385,420,455,490,525,560,595,630,665,700,
36,72,108,144,180,216,252,288,324,360,396,432,468,504,540,576,612,648,684,720,
37,74,111,148,185,222,259,296,333,370,407,444,481,518,555,592,629,666,703,740,
38,76,114,152,190,228,266,304,342,380,418,456,494,532,570,608,646,684,722,760,
39,78,117,156,195,234,273,312,351,390,429,468,507,546,585,624,663,702,741,780,
40,80,120,160,200,240,280,320,360,400,440,480,520,560,600,640,680,720,760,800,
41,82,123,164,205,246,287,328,369,410,451,492,533,574,615,656,697,738,779,820,
42,84,126,168,210,252,294,336,378,420,462,504,546,588,630,672,714,756,798,840,
43,86,129,172,215,258,301,344,387,430,473,516,559,602,645,688,731,774,817,860,
44,88,132,176,220,264,308,352,396,440,484,528,572,616,660,704,748,792,836,880,
45,90,135,180,225,270,315,360,405,450,495,540,585,630,675,720,765,810,855,900,
46,92,138,184,230,276,322,368,414,460,506,552,598,644,690,736,782,828,874,920,
47,94,141,188,235,282,329,376,423,470,517,564,611,658,705,752,799,846,893,940,
48,96,144,192,240,288,336,384,432,480,528,576,624,672,720,768,816,864,912,960,
49,98,147,196,245,294,343,392,441,490,539,588,637,686,735,784,833,882,931,980,
50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000,
---------------

Este es mi codigo:

public static void main(String[] args) {
    int v []= new int [50];
    int p []= new int [20];
    int m[][]=new int[50][20];


    for (int i = 0; i < 50; i++) {
        v[i] = (i + 1);
    }


    for (int i = 0; i < 20; i++) {
        p[i] = (i + 1);
    }

    juntarVectores(v,p,m);
    Matriz(v,p,m);

}

 public static void Matriz(int []v,int []p,int[][]m) {

        System.out.println("Esta es tu matriz juntando ambos vectores:");
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                System.out.print(m[i][j] + ",");
            }
            System.out.println();
        }

    }

    public static int[][] juntarVectores(int []v,int[]p,int[][]m) {

        int[][] myJoinedArray = new int[v.length][p.length];

        for (int i = 0; i < myJoinedArray.length; i++) {
            for (int j = 0; j < myJoinedArray[i].length; j++) {
                myJoinedArray[i][j] = v[i] * p[j];
            }
        }

        m = myJoinedArray;
        return m;

    }

pero a mi por consola me sale lo siguiente:

Esta es tu matriz juntando ambos vectores:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Que estoy realizando mal? alguien me podría ayudar?

1
  • ¿Si te funcionó mi solución?
    – MrDave1999
    Commented el 23 nov. 2019 a las 15:42

3 respuestas 3

1

Te faltó reasignar el valor de m

public static void main(String[] args) {
    int v[] = new int[50];
    int p[] = new int[20];
    int m[][] = new int[50][20];

    for (int i = 0; i < 50; i++) {
        v[i] = (i + 1);
    }

    for (int i = 0; i < 20; i++) {
        p[i] = (i + 1);
    }        

    m = juntarVectores(v, p, m); // <--- AQUI
    Matriz(v, p, m);
}
1
  • Muchísimas gracias justo era eso lo que no sabía cómo colocar :)
    – user148424
    Commented el 23 nov. 2019 a las 0:31
1

Voy a explicarte el error con un ejemplo. Supongamos que tenemos el siguiente código con una función que calcula el doble de un numero:

public static void main(String[] args) {
    int a = 10;

    calcularDoble(a);

    System.out.println(a);
}

public static void calcularDoble(int a) {
    int b = a * 2;

    a = b;
}

Esperamos que luego de ejecutar calcularDoble, a sea 20, pero con lo que nos encontramos es que a sigue siendo 10. Esto pasa porque dentro de la función calcularDoble, a es otra variable y no la que tenemos en main, por eso al intentar asignarle algo solo la cambia dentro de calcularDoble. Si queremos que cambia el a dentro de main tenemos que devolver el resultado en calcularDoble y asignarlo dentro de main de la siguiente forma:

public static void main(String[] args) {
    int a = 10;

    a = calcularDoble(a);

    System.out.println(a);
}

public static int calcularDoble(int a) {
    int b = a * 2;
    return b;
}

En tu caso haces lo mismo pero con juntarVectores. Al hacer:

m = myJoinedArray;

Estas cambiando la variable m dentro de juntarVectores y no en main, por eso te muestra la matriz con ceros (el valor por defecto).

1
  • 1
    Muchísimas gracias por explicarme mi error, gracias a ti he podido comprender el fallo correctamente.
    – user148424
    Commented el 23 nov. 2019 a las 0:39
1

Ya te dieron una solución y también te explicaron la razón del porqué tu programa no funcionaba correctamente; sin embargo, te daré otra solución.

Si te das cuenta, todo este código está bien:

public static void main(String[] args) {
    int v []= new int [50];
    int p []= new int [20];
    int m[][]=new int[50][20];


    for (int i = 0; i < 50; i++) {
        v[i] = (i + 1);
    }


    for (int i = 0; i < 20; i++) {
        p[i] = (i + 1);
    }

    juntarVectores(v,p,m);
    Matriz(v,p,m);

}

Pero la parte más interesante es esta línea:

juntarVectores(v,p,m);

La variable m hace referencia a un objeto de tipo int[][], esto quiere decir que cuando llamas al método juntarVectores, en realidad estarás pasando implícitamente la dirección base (es decir, del primer elemento) del objeto que tenga almacenada la variable m (debemos recordar que la variable m es un puntero implícito, aunque en Java no haya soporte para punteros, siempre existen internamente).

Entonces, entendiendo este concepto básico, podríamos decir que si le pasamos la referencia que tiene guardada m (la variable del main) al parámetro m (del método juntarVectores), el método podría modificar el contenido de la matriz que se haya pasado por referencia de forma implícita (digo implícito porqué en Java no se puede pasar directamente la dirección de memoria de una variable; sin embargo, el compilador se encarga de pasar la dirección que tenga almacenada el puntero m).

Es decir, el método juntarVectores quedaría de esta forma:

public static void juntarVectores(int []v, int[]p, int[][]m)
{
    for (int i = 0; i < m.length; i++) 
    {
         for (int j = 0; j < m[i].length; j++)
                m[i][j] = v[i] * p[j];
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.