Print vector disregarding repeated values

Asked

Viewed 3,965 times

3

Make an algorithm that reads a 50-element vector and prints the vector disregarding the repeated values.

The code is giving the correct output, but if I type a repeated number, the program requests a new one until I type one that does not yet exist in the array. How to fix this?

int main(){
int vet[5], i, j, num;
for(i = 0; i < 5; i++){
     cout << ("Numero: ");
     cin >> vet[i];
     num = vet[i];
     for(j = 0; j < i; j++){
        if (vet[j] == num){
            --i;
            }
        }
     }

for(i = 0; i < 5; i++){
    cout << vet[i] << endl;
}
return 0;
}
  • 6

    I will consider that the statement is exactly this. It already begins that you are not doing what the statement asks. Note that it has two parts: 1) "read a 50 element vector" (you are much more than that in loop which should be very simple; 2) "print the vector disregarding the repeated values" (you are printing everything). It may sound silly, but it makes a difference. Programming is like this, you have to be aware of all the details. There this second loop is that you should check if that value already exists. So probably if you do this, you will already know how to fix the problem loop.

1 answer

5


I will consider that the statement is exactly this. You are not doing what the statement asks. Note that it has two parts:

  1. "read a 50-element vector" (you’re doing much more than that on loop that should be very simple;
  2. "print the vector by disregarding the repeated values" (you are printing everything).

It may sound silly, but it makes a difference. Programming is like this, you have to be aware of all the details. There this second loop is that you should check if a value already exists.

Solving this, you will automatically not have the problem of loop get stuck.

Without modifying much and optimizing in the best way possible (but optimised in the most obvious way) I did a quick test that solves the way it was proposed. I didn’t get to do any extensive tests. I also didn’t modify your code much or improve the formatting of the output, I think this you know how to turn.

#include <iostream>
using namespace std;
#define MAX_ITENS 50 //número de itens a serem lidos se quiser modificar para testes

int main() {
    int vet[MAX_ITENS];
    bool repetido;
    for(int i = 0; i < MAX_ITENS; i++) {
         cout << ("Numero: ");
         cin >> vet[i];
    }
    for(int i = 0; i < MAX_ITENS; i++) {
        repetido = false;
         for(int j = 0; j < i; j++) { //verifica se existem iguais apenas entre os anteriores
             if (vet[j] == vet[i]) {
                repetido = true;
                break; //tem repetido, não precisa continuar com a procura
             }
         }
         if (!repetido) {
             cout << vet[i] << endl;
         }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Bigown, this way the repeat shouldn’t always be true, right? It looks because, you compare vet[i] with vet[j], initially both have the same index. That is, looking fast, it seems that you compare the same vector...

  • 1

    No. At no time iand j have the same number. This test has been done.

  • Ball show, @bigown. Sounds like a beast, but there are simple things that catch us. Thank you very much.

  • It’s normal when you’re learning.

Browser other questions tagged

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