Error adding value in array, repeated values

6

I have two arrays, A and B.

I have created 3 functions in which I take a value from the beginning of array A and array B, remove the values, compare the values and if the value of A is greater than B, I add the 2 values at the end of array A, or else I add to list B. The problem is that one of the functions is giving error and I can not find the error. When I give a print in the array, arrays that have unique values, then have repeated values. Can anyone help me find and fix the error?

This function takes a card from the beginning of the array. SE is -1, it's because the index is empty, and then you should get the next available letter.

  int getValorInicio (int[] arrCards, int topPonteiro) { 
    int card=0;

    if (arrCards[topPonteiro]!=-1) {
       card=arrCards[topPonteiro];
    }else{
       for(int i=0; i<arrCards.length; i++){
          if (arrCards[i]!=-1){
             card=arrCards[i];
             break;
          }
       }
     }
     return card;
}

This function removes the letter, and indicates the next empty index.:

int removeCard(int[] arrCards, int topPonteiro) { 
  if(arrCards[topPonteiro] != -1){
    arrCards[topPonteiro]=-1; 
    topPonteiro=(topPonteiro+1)%arrCards.length;    
  } 
  return topPonteiro;
}

And finally this function adds and returns the next free position, but it seems like it adds several times the same value, when it should not.

    int addFinalArr(int [] arrCards, int ultimoPonteiro, int card) {
       int nextPosition=0;
       if (arrCards[ultimoPonteiro]==-1 && ultimoPonteiro!=ultimoPonteiro+1) {
         arrCards[ultimoPonteiro]=card; 
       }
       else{
          for(int i=0; i<arrCards.length; i++){   
             if (arrCards[i]==-1 && arrCards[i]!=arrCards[0]){
                nextPosition=i;
                break;
             }
             nextPosition=i;
          }
      }   
   return nextPosition;  
}
    
asked by anonymous 23.03.2018 / 21:59

1 answer

1

I understood your problem but looking at your code things got a little confusing, what you need and understand the concepts of queue, stack and list of data structure and implement them (give a studied and very useful). >

Remove from the first and insert in the last element is from the FILA concept, however its FILA is customized because it removes an element from the first of each array and inserts in the last of array1 or array2 according to your rule.

In Java you will have to do the methods returning the new arrays since this language does not work with pointers (you can not access an array from within a invoked method), in short it would look like this.

 public static void main(String[] args) {
    //o zero(0) representa casas vazias repare que o 
    //cartasA tem o primeiro elemento maior (5).
    int[] cartasA = {5,3,4,5,10,2,2,5,6,5,5,4,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int[] cartasB = {3,3,4,5,10,2,2,5,6,5,5,4,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

    //imprime array cartasA antes de processar
    for (int i : cartasA) {
        System.out.print(i+",");
    }

    //armazena os dois primeiros elementos para adicionar mais tarde
    int tmpA = cartasA[0];
    int tmpB = cartasB[0];

    //remove o primeiro elemento
    cartasA = removerPrimeiro(cartasA);
    cartasB = removerPrimeiro(cartasB);

    //identifica qual array deve inserir os elementos no fim
    if(tmpA > tmpB){
        cartasA = inserirFim(cartasA, tmpA, tmpB);
    }else{
        cartasB = inserirFim(cartasB, tmpB, tmpA);
    }

    //imprime array cartasA depois de processar        
    System.out.println("");
    for (int i : cartasA) {
        System.out.print(i+",");
    }

}

static int[] removerPrimeiro(int[] cartas){
    //se o array estiver vazio retorna ele mesmo
    if(cartas[0] == 0)
       return cartas;

    //percorre o array deslocando os elementos para esquerda
    for (int i = 0; i < cartas.length - 1; i++) {
        cartas[i] = cartas[i+1];
    }
    //remove o ultimo elemento que o for não pode alcançar
    cartas[cartas.length - 1] = 0;
    return cartas;//retorna o novo array sem o primeiro valor
}

static int[] inserirFim(int[] cartas, int a, int b){
    //percorre o array em busca do ultimo elemento
    for (int i = 0; i < cartas.length - 1; i++) {
        if(cartas[i] == 0){
            //insere os dois valores nas duas ultimas posições
            cartas[i] = a;
            cartas[i + 1] = b;
            break;
        }
    }
    return cartas;//retorna o novo array
}

Output from the array lettersA:

Before you run

[ 5 , 3,4,5,10,2,2,5,6,5,5,4,6,0,0,0,0,0,0,0 , 0.0,0,0,0,0,0] (repair the 5)

After you run

[3,4,5,10,2,2,5,6,5,5,4,6, 5 , 3 , 0,0, 0.0,0,0,0,0,0,0,0,0,0] (note that 5 left and entered 5 at the end along with 3 of the lettersB)

I hope you have helped.

UPDATE

Try this remove

/**
 * neste caso os arrays cartasA e cartasB devem estar declarados static
 * para o metodo conseguir acessar nele, e fazer os metodos separados
 * um para cada baralho
 * @param cartas
 * @return 
 */
static int removeCartasA(){
    int resp = -1;
    for (int i = 0; i < cartasA.length; i++) {
        //procura valor -1
        if(cartasA[i] != -1){
            resp = cartasA[i];//armazena valor para retornar
            cartasA[i] = -1;//remove substituindo com -1
            break;//sai do loop
        }
    }
    return resp;//retorna o valor removido
}

static int removeCartasB(){
    int resp = -1;
    for (int i = 0; i < cartasB.length; i++) {
        //procura valor -1
        if(cartasB[i] != -1){
            resp = cartasB[i];//armazena valor para retornar
            cartasB[i] = -1;//remove substituindo com -1
            break;//sai do loop
        }
    }
    return resp;//retorna o valor removido
}
    
27.03.2018 / 22:03