Vector comparison program C++

Asked

Viewed 143 times

0

You guys all right? I’m beginner and I’m trying to build a program that compares two vectors and if all the elements of the two vectors are. I’ll exemplify it to get better...

vector 1 = 10, 20 , 30 and vector 2 = 10 20 30; then the output will be: equal numbers.

vector 1 = 10, 20, 20 and vector 2 = 10, 20, 15; then the output would be: 10 20 and tbm saying these were the equal numbers.

vector = 10, 20, 30 and vector 2 = 15, 19, 4; so the output would be: no equal number. the problem is that when all numbers are equal, apart from him give out saying that all are equal he tbm ta showing the numbers and it is not for that happen.

 #include <iostream>
using namespace std;

int main() {

  int n;
  cin >> n;

  int vetorX[n], i, a,c[n];

  for(a=0; a<n; a++){
  cin >> vetorX[a];
  }

  int vetorC[10] = {19, 7, 14, 0, 11, 17};
  int cont =0, cont1 = 0, cont2 =0;
  
 
  for(i=0; i<n; i++){
      for(a=0; a<n; a++){
        //conta a quantidade de numeros iguais
          if(vetorX[a] == vetorC[i]){
            cont ++;
            c[n] = vetorX[a];
            cout << c[n];
          }
      }
  }

  if(cont >= 7){
    cout << "todos iguais";
  }

  if(cont > 1 && cont < n){
    cout << "numeros iguais:";  
  }

  if(cont == 0){
    cout << "nenhum igual";
  }

}
  • It seems to me that if you count == 0 then you have none equal and if you count < 7 then there are some equal but not all. A situation that should be analyzed is what the result will be obtained if there is a repetition of some element in the vector(s) and what to do in this case.

  • I took his tip from scratch and it worked and the rest tbm, but if all the elements are equal it returns that are equal and displays the numbers tbm and I don’t want that to happen, I want that if all the numbers are equal it returns that, all are equal, without showing the numbers.

  • I don’t see the point in using 3 commands if with exactly the same condition.

  • Yeah, I didn’t even realize it, so I took it. The problem is that it keeps displaying the numbers even if all the numbers are the same and it is not to roll this, it would be the case to put a condition for it to only display the numbers if there is any difference?

1 answer

1

Hello, you can post your code on Compiler explorer that gets easier.

C++ has a number of techniques to make your life easier

1 - Instead of using "[]" vectors use the "Std::vector" vector in C++

2 - It has a lot of overloaded operators (==, !=, <, >, <=, >=) so you can directly ask v1 == v2 or v1 != v2.

3 - It has initialization by {} the same way as the vectors []

    vector a = { 10, 20, 30, 40 };
    vector b = { 10, 20, 30, 40 };

    if ( a == b ) {  // pergunte diretamente se os vetores são iguais
       std::cout << "iguais";
    } else { 
       std::cout << "diferentes";
    }

4 - Learn to use the algorithms and containers of stl, already has an algorithm ready Std:: to do what you want, and besides, you can easily write code without loops "for" ( more readable )

What you want to do is a little unusual: generate an output with equal values only if the vector has some different number ? In this case you will have to pass 2 times in the vectors, one to see if they are different and another to generate the output if they are. Even so :

Full example https://godbolt.org/z/4j35nx

There in the code you will see "Mismatch()" which makes clear its intention to search for the first different element in 2 containers and will also see "copy" with last Cout operator, ie copy to the output.

Otherwise there is the case that one vector is a subset of the other, a = { 10, 20 } and b = {10, 20,30} vc wants which output in this case ?

Hugs

Browser other questions tagged

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