6
I have two arrays, A and B.
I 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 that of B, I add the 2 values at the end of array A, or else I add it to the list B. The problem is that one of the functions is giving error and I can not find the error. When I print the array, the arrays that had unique values have repeated values.Someone can help me find and fix the error?
This function takes a card from the beginning of the array. IF it is -1, it is because the index is empty, and then should take the next available card.
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 card, and indicates the next empty tile.:
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 that 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;
}
You remove read values from arrays before making your comparisons?
– Leonardo Alves Machado
Yes. I take the values, compare and then add to the list that had the highest value letter.
– find83