I can’t generate that second vector

Asked

Viewed 62 times

1

Write a program that loads a vector with 10 integers and sort it in ascending order using the Bubble Sort method.

The program must generate a second vector without repeated numbers.

package com.bubble.sort;
import java.util.Scanner;

public class bubble_sort {
    private static Scanner teclado;

    public static void main(String args []) {
        int vet1[] = new int [10];
        int vet2[] = new int [10];
        int n, i, aux,troca;

        teclado = new Scanner(System.in);
        //Carregando os números do vetor:
        for(i=0;i<=9;i++) {

            System.out.print("Digite "+(i+1)+"º número do vetor:");
            vet1[i]= teclado.nextInt();
            vet2[i] = vet1[i];
        }
        n = 1;
        troca = 1;
        while (n <= 10 && troca == 1) {

            troca = 0;
            for(i=0;i<=8;i++) {

                if(vet1[i] > vet1[i+1]) {

                    troca = 1;
                    aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;

                }

            }
            n = n + 1;
        }

        for(i=0;i<=9;i++) {
            System.out.print("["+vet1[i]+"]");
        }

    }
}

1 answer

0


Remembering the remaining positions of the array that are not used, and should be ignored when displaying the array.

int valorAnterior =  vet1[0];
int contatodor = 0;
for(i=1;i<=9;i++) {
  if(valorAnterior != vet1[i]){
    vet2[contatodor] = vet1[i];
    valorAnterior = vet1[i];
    contatodor++;
  }
}

Complete code:

package com.bubble.sort;
import java.util.Scanner;

public class bubble_sort {
    private static Scanner teclado;

    public static void main(String args []) {
        int vet1[] = new int [10];
        int vet2[] = new int [10];
        int n, i, aux,troca;

        teclado = new Scanner(System.in);
        //Carregando os números do vetor:
        for(i=0;i<=9;i++) {

            System.out.print("Digite "+(i+1)+"º número do vetor:");
            vet1[i]= teclado.nextInt();
            vet2[i] = vet1[i];
        }
        n = 1;
        troca = 1;
        while (n <= 10 && troca == 1) {

            troca = 0;
            for(i=0;i<=8;i++) {

                if(vet1[i] > vet1[i+1]) {

                    troca = 1;
                    aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;

                }

            }
            n = n + 1;
        }

        for(i=0;i<=9;i++) {
            System.out.print("["+vet1[i]+"]");
        }

        int valorAnterior =  vet1[0];
        vet2[0] =  vet1[0];
        int contatodor = 1;
        for(i=1;i<=9;i++) {
          if(valorAnterior != vet1[i]){
            vet2[contatodor] = vet1[i];
            valorAnterior = vet1[i];
            contatodor++;
          }
        }

        for(i=0;i<contatodor;i++) {
            System.out.print("["+vet2[i]+"]");
        }

    }
}
  • It’s getting the same vector.

  • The second verse?

  • Could make the full code?

  • Complete code posted.

  • There is no way to take the 0 in the repeated position not right?

  • made the change, now will not show the values 0

  • And you’re losing the first value every time!

  • Dude, I made a correction, now you’re getting the first number. @Kuriôzu

  • In mine it’s the same.

  • The modification was very small, you took the new code?

  • yes, but still not showing the 1st value of the 2 generated vector.

  • Try it now, please.

  • Now the first one appears, but a number appears after the numbers of the vector: EX.: [1],[2],[1]

  • Okay buddy, sorry about this much change is because I’m in the service and I don’t have an ide here, but I believe it will now work 100%. @Kuriôzu

  • 1

    100%, dude helped a lot.

  • Mark the answer as answered, please.

Show 11 more comments

Browser other questions tagged

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