How to check and print repeat values in a vector

Asked

Viewed 2,499 times

0

I have the following problem to check the repeating vectors in a Vector:

Given a sequence of n real numbers, determine the numbers that make up the sequence and the number of times each of them occurs in it.

Example:

n = 8

Sequel:

-1.7, 3.0, 0.0, 1.5, 0.0, -1.7, 2.3, -1,7

Exit:

-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’s the code I wrote so far 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;
}	

So far I’m having the following result:

inserir a descrição da imagem aqui

the problem is that the repeated values are being shown in the amount of times they contain within the vector, and not just one as asks for the exercise.

:/

3 answers

0

The problem is that you are assigning an 8 value to N, let’s say your vector:

vet[0] = -1.7
vet[1] = 3
vet[2] = 0
vet[3] = 1.5
vet[4] = 0
vet[5] = -1.7
vet[6] = 2.3
vet[7] = -1.7

So every time he will check, because in his for he will go through this vector 8 times, for example in his case at position 0 5 and 7 of his vector, we have the value -1.7, so when he falls in the second for it will do this check at positions 0, 5 and 7. Also note that your command to print cout is just after the second repeat command ends and before the first will start again then it will print all 8 times.

-1

There is a "C++ way that is not C" to do some things, whenever you can use C++ prefer :

1 - Use containers stl : Std::vector instead of vectors "float vet[n]", because the allocation and misallocation are automatic.

2 - Use Std::map for values that must be "indexed" by search keys

vector<float> vet;
map<float, int> result;

3 - Use stl algorithms and iterators instead of "for ( int.." pq makes clear your intention ) Std::copy, Std::Transform, Std::find... for example to copy n elements of an entry use Std::copy_n( in... ), to insert into an array, use Std::back_inserter :

copy_n(istream_iterator<float>(in), n, back_inserter(vet));  // le n elementos float de in e insere no vetor.

4 - Maps are native search structures: The bottom line has 2 commands: one is result[v] that searching v in result, if not found creates a default int() (initialized with 0) on the map with the 'v' key that you used to search and returns a reference to it, if it finds returns a reference to the found item, anyway, the map ensures that there will be a valid reference in the answer. The other part is ++ that will run in the result of the first part. Adding perfectly 1 in the item you are looking for.

++result[v]

5 - To generate the result the is standard maps :

 for( const auto& [k, v] : result) {
   cout << k << " -> " << v << endl;
 }

Example in Compiler explorer https://godbolt.org/z/MzPcc1

-1


Buddy, I haven’t programmed for C in a while, but I did what you need in javascript, the logic is the same

var array = [1,2,3,2,4,5,6,3,6,8,7,4]
var arrayVerificado = [];

for(var i = 0; i < array.length; i++){
	var contador = 0;  
  var repetiu = false;
  
  for(var z = 0; z < arrayVerificado.length; z++){
  	if(arrayVerificado[z] == array[i]){
     //Verifico se no array auxiliar, o número já foi verificado, caso sim, seto a variavel repetiu para true.
    	repetiu = true
    }
  }  
  if(repetiu == false){ //caso não tenha repetido, eu deixo o padrão false.
  arrayVerificado.push(array[i])//add no meu array auxiliar que este número vai ser verificado
    for(var j = 0; j < array.length; j++){
      if(array[i] == array[j]){ //se ele achar, faço meu contador acrescentar +1
        contador++
      }
      
    }
      console.log("O número: "+ array[i]+ " repetiu: "+ contador + " vezes");
      }

}

Browser other questions tagged

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