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;
}