Help with a exercise (with code)

how do I check the value in a vector?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
     #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
 int verifica (int x){
    int N; 
   for
            }
      }while(nota<0||nota>10);
    return nota;
}
  int main(){
      int N;
      cout<<"informe o tamanho do vetor: ";
      cin>>N;
      int vet[N];//criando vetor de tamanho variável
      int i,j,aux;
      srand(time(NULL));
      for(i=0;i<N;i++)
         vet[i]=rand()%100;
      //mostra
      for(i=0;i<N;i++)
         cout<<vet[i]<<"\t";
      cout<<endl;  
      //ordena
      for(i=0;i<N-1;i++){
         for(j=i+1;j<N;j++){
             if(vet[i]>vet[j]){
                 aux=vet[i];
                 vet[i]=vet[j];
                 vet[j]=aux;
             }
         }
      }
     //mostra
      for(i=0;i<N;i++)
         cout<<vet[i]<<"\t";
      cout<<endl;
      system("pause");
    }

Last edited on
What do you mean by "check the value"?
check a value in the vector for dont show equal numbers
Last edited on
Do you mean checking if two vectors are equal, or if any two numbers in a single vector are equal?
checking if any two numbers in a single vector are equal
Iterate through the vector comparing each element with all elements after it; if at any point they are equal, then the vector contains two numbers that are equal. If you make it through without that happening, then all the elements are distinct.
Could you give me an example using a code?
To do Zhuge's suggestion, all you need to do is something like this:

1
2
3
4
5
6
7
8
9
10
for(i = 0;i < yourvectorname.size();++i)
    {
        for(j = i + 1;j < yourvectorname.size();++j)
        {
            if(yourvectorname.at(i) == yourvectorname.at(j))
            {
                std::cout << "Two elements with the same value found at " << i << " and " << j << "!\n";
            }
        }
    }


A more pressing issue here is that you are NOT USING ANY VECTORS IN YOUR CODE ANYWHERE!
Last edited on
Topic archived. No new replies allowed.