I have the following problem to check the vectors that are repeated in a Vector:
Given a sequence of n real numbers, determine the numbers that make up the sequence and the number of times each occur in it.
Example:
n = 8
Sequence:
-1.7, 3.0, 0.0, 1.5, 0.0, -1.7, 2.3, -1.7
Output:
-1.7 occurs 3 times
3.0 occurs 1 time
0.0 occurs 2 times
1.5 occurs 1 time
2.3 occurs 1 time
Here is the code I wrote here in C ++:
#include <iostream>
using namespace std;
int main () {
int n, i, i2, i3, a, cont=0;
cin>>n;
float vet[n], vet2[n], vet3[n];
for(i=0 ; i<n ; i++){
cin>>vet[i];
vet2[i]=vet[i];
}
for(i=0 ; i<n ; i++){
cont=0;
for(i2=0 ; i2<n ; i2++){
if(vet[i]==vet2[i2]){
cont++;
vet3[i]=vet[i];
}
}
cout.precision(1);
cout<<fixed<<vet[i]<<" ocorre "<<cont<<" vezes"<<endl;
}
return 0;
}
At the moment I'm having the following result:
The problem is that the repeated values are being shown in the amount of times it contains within the vector, not just one as it asks for the exercise.
: /