Comparison of Vector regardless of position

Asked

Viewed 450 times

4

#include<stdio.h>


main(){

    int vetoresIguais= 0;

    int vetor[3] = {10,20,5};

    int vetor1[3]= {20,50,15};

    int i;

    int j;

    for(i = 0; i < 3; i++){

        for(j = i; j < 3;j++){

            if(vetor[i] == vetor1[i]){

                vetoresIguais++;
            }
        }
    }

    printf("%d",vetoresIguais);
}

The result of this code is 0,why is it comparing the vector to only one position,how to solve so that compare all values and tell how many values are repeated regardless of the position be equal or not?

(Feel free to edit, never learned kk,I’m new around here)

  • because you also need to execute both commands for for vector 1, so that it looks in each of its elements. It would be better to put this in a Function, already learned how to do this?

  • When comparing the vectors, change the i of vector 1 by j, becoming vector 1[ j ]

3 answers

5


Cardinality of the intersection of two vectors in C

Depending on the number of vector elements, another more efficient strategy may be employed. The one below is simple, you go through each element of one of the vectors and check how often this element occurs in the other vector. The difference from the previous answer is that it does not require the elements of the vectors to be distinct (i.e., there may be repetitions).

Its complexity is O(N2), commented on in another answer, which means that for "large" vectors, as already anticipated, you may have to look for another alternative. For example, if one of the vectors is ordered, then it is not necessary to go through all of it to locate a given value.

for (i = 0, i < 3; i++) {
    for (j = 0; j < 3; j++) {
       if (vetor[i] == vetor1[j]) {
         totalComponentesIguais++;
       }
    }
}

IMPORTANT: this solution does not require the elements of the vectors to be distinct.

4

The correct way to compare 2 vectors is to take the i position of the 1st vector and compare with all the positions of the 2nd vector.

for(i = 0; i < 3; i++){
    for(j = 0; j < 3; j++){
        if(vetor[i] == vetor1[j]){
            vetoresIguais++;
            break;
        }
    }
}

This algorithm is O(N 2), probably there is a faster solution, but this is easy to understand


Ex:

V1 |10,5,2|

V2 |5,6,10|

We took the elements from V1 and compare with all of the V2

1st iteration: 10 equals 5? No (i=0, j=0)

2nd iteration: 10 equals 6? No (i=0, j=1)

3rd iteration: 10 equals 10? Yes (i=0, j=2)

Then we take 5 and do the same

1st iteration: 5 equals 5? Yes(i=1, j=0)

break (stop the cycle of J and proceed to the next iteration of I)

We took the 2

1st iteration: 2 equals 5? No (i=2, j=0)

2nd iteration: 2 equals 6? No (i=2, j=1)

3rd iteration: 2 equals 10? No (i=2, j=2)

...etc.

2

By readjusting your code:

#include<stdio.h>

int main(void)
{

int vetor[3] = {10,20,5};
int vetor1[3]= {20,50,15};
int i, j, vetoresIguais;

for(i = 0, vetoresIguais = 0; i < 3; i++)
{
    for(j = 0; j < 3; j++)
    {
        if(vetor[i] == vetor1[j])
        {
            vetoresIguais++;
        }
    }
}

printf("%d",vetoresIguais);

return 0;
}

The variable vectoriesIguais would be better named as elementsIguais.

  • The problem, as presented initially, persists in this solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.