0

My main default is the logic for the analysis. What is demanded. The processing algorithm in the analysis function is the main interest of the exercise.

I need to compare the player proposition called essai[] and the ref[]. The ref[] is a 4-digit number made by the random function. One number counted as "exactly in the right place" can't be counted as "right number in wrong place". Exemple: ref[] = {1, 2, 3, 4} and essai[] = {1, 5, 7, 3}. It will count as 1 number in "exactly right place" and 0 numbers in "wrong place". Cannot be counted more then once. Same for the other way. I started with the right place, if there is then I replace both numbers, in essai[] and ref[] with -1. I did the same for "right number in wrong place".

Here's my code so far:

void analyse(int ref[], int essai[]) {
int i, j, o, RefTmp[4], NbPos, NbChif;

for(i = 0; i < 4; i++){
  RefTmp[i] = ref[i];
}

for (j = 0; j < 4; j++) { // [1] = 5 : [1] = 2
    for (o = 0; o < 4; o++) {
        if (RefTmp[j] == essai[o]) {
            if (j == o) {
                ++NbPos;
                RefTmp[j] = essai[o] = -1;
                printf("One number in the right place\n");
                break;
            }
            else {
                ++NbChif;
                RefTmp[j] = essai[o] = -1;
                printf("One number in the wrong place\n");
                break;
            }
        }
    }
}
}

What I understand is that I need to make 2 for loops to compare both arrays, but when there's multiple "right numbers in wrong", (for example), the loop prints multiple times too. It must only print once. I made a RefTmp[] so that the real ref[] won't be destroyed in the process.

user438383
  • 5,716
  • 8
  • 28
  • 43
MimiValsi
  • 11
  • 2

1 Answers1

0

You should find & "erase" all of the "right number right place" instances (which only needs a single loop) before even considering handling the "right number wrong place" instances.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Do you mean that I use only one loop and if there's the same number I replace it with NULL? like RefTmp[1] = essai[1] = NULL? Sry for the question, I only have like 1 month in C language ^^' – MimiValsi Dec 28 '21 at 18:57
  • Wasn't the point of assigning -1 to "erase" the actual value? – Scott Hunter Dec 28 '21 at 20:08
  • Well, if I think about it, I guess. But when I replace or erase that number by -1, the loop will see an equality between both arrays non? :| – MimiValsi Dec 28 '21 at 20:35
  • Then why were you doing it before? If you "erase" using a different value for each array, would that solve the problem? – Scott Hunter Dec 28 '21 at 20:55
  • Was doing it because the exercise told me to. ^^ I may need to rethink the process differently. Thx for the help ^^' – MimiValsi Dec 28 '21 at 21:03