Variable does not increase me and I do not see the fault

1

Good, I am comparing a vector with each of the rows of a matrix. I have created a variable to indicate when it has reached the end of the vector so that it does not continue comparing. This variable (called coincidence) increases every time I find a match but it does not increase its value and I do not understand why. By not increasing, I do not enter the if that compares it with the length of the vector and keeps comparing me. Could someone tell me my mistake or give me a clue about the solution? Thanks in advance.

    int[][] matriz = {{2, 4, 6}, {1, 2, 4, 5, 5}, {1, 8, 9}};
    int[] vector = {2, 4, 5};

    int coincidencia = 0;


    for (int i = 0; i < matriz.length; i++) {
        for (int j = 0; j < matriz[i].length; j++) {

            if (vector.length  <= matriz[i].length ) {

                for (int k = 0; k < vector.length; k++) {

                    if (vector[k] == matriz[i][j]) {
                        System.out.println("Coincidencia en " + "[" + i + "]" + "[" + j + "]");
                        coincidencia++;
                        System.out.println(coincidencia);
                    } else {
                        coincidencia = 0;
                    }
                }
                if(coincidencia == vector.length) {
                    break;
                }
            } else{
                break;
            }
         }
    }
    
asked by AGeekM 16.09.2017 в 18:44
source

3 answers

1

Once again you have a logic problem in your algorithm ...

you're looking for matches, and you're doing it wrong ...

Let's analyze your code (again, we've done it several times):

for (int i = 0; i < matriz.length; i++) {
//recorres las filas de la matriz
    for (int j = 0; j < matriz[i].length; j++) {
    //recorres las columnas de la matriz
        if (vector.length  <= matriz[i].length ) {
        //compruebas si el vector es menor que la fila de la matriz, bien
            for (int k = 0; k < vector.length; k++) {
            //recorres el vector....
                if (vector[k] == matriz[i][j]) {
                //comparas el vector, contra la posicion de la matriz.. pero todo el vector?
                //contra la misma posicion de la matriz? entonces aqui esta el error.

Your solution should be something like this:

int[][] matriz = {{2, 4, 6}, {1, 2, 4, 5, 5}, {1, 8, 9}};
int[] vector = {2, 4, 5};

int punteroalvector = 0;
for (int i = 0; i < matriz.length; i++) 
{
   for (int j = 0; j < matriz[i].length; j++) 
   {
       if (vector[punteroalvector] == matriz[i][j]) 
       {
           punteroalvector++;
           if (punteroalvector == vector.length)
           {
              System.out.println("encontre");
              System.out.println("i: " + i + " j desde: " + (j-punteroalvector) + " j hasta: " + j);
              break;
            }
        }
        else
        {
            punteroalvector = 0;
        }
    }
    if (punteroalvector == vector.length)
    {

         break;
    }
}     
    
answered by 16.09.2017 / 20:29
source
1

First, the problem of why it's not working for you:

If you notice you have 3 for nested, and where you make the comparison is in the innermost, that is, you are comparing each of the elements of the vector with the first of the elements of the row of the matrix. Then all the elements of the vector with the second of the row of the matrix, and so on. The problem is that there is an iterator left over. You must iterate through the ith row by comparing the j-th elements of the vector and that row.

I think this code should solve the problem.

int[][] matriz = {{2, 4, 6}, {1, 2, 4, 5, 5}, {1, 8, 9}};
int[] vector = {2, 4, 5};

int coincidencia = 0;

for (int i = 0; i < matriz.length; i++) {

    if (vector.length  == matriz[i].length ) {
        //si en esta fila no hay la misma cantidad de elementos, pasa a la siguiente

        coincidencia = 0;   //reset de la variable que actua como contador
        for (int j = 0; j < matriz.length; j++) {

            if (vector[j] == matriz[i][j]) {
                System.out.println("Coincidencia en " + "[" + i + "]" + "[" + j + "]");
                coincidencia++;
                System.out.println(coincidencia);
            } else {
                //si uno fallo, ya no es el vector lo que hay en esta fila
                break;
            }

        }

        if (coincidencia == vector.length) {
            //encontramos el vector!
            break;
        }   
    }   
}

The other problem I see in the code you are using is that you have the if nested within the second for , so that comparisons are made to the size of the row that are unnecessary.

And finally, the condition of if is actually that the row of the matrix is exactly equal to the size of the vector you are looking for. This will speed up and simplify the code a lot.

I hope I have helped.

    
answered by 16.09.2017 в 20:30
1

Sorry, coincidentally you mean that in the matrix the vector is true, if that is the case then it is much easier in this way:

int[][] matriz = {{2, 4, 6}, {1, 2, 4, 5, 5}, {1, 8, 9}};
    int[] vector = {2, 4, 6};

    int coincidencias = 0;
    for (int i = 0; i < matriz.length; i++) {
        if(Arrays.equals(matriz[i], vector)){//Claramente Arrays.equals compara 2 arreglos de cualquier tipo para ver si sus elementos son los mismos.
            coincidencias++;
        }
    }

    JOptionPane.showMessageDialog(null, "Coincidencias: "+coincidencias);
    
answered by 16.09.2017 в 21:04