Problem with comparing arrays

I am trying to compare two arrays and see how many are same and in same position. Also, see how many are same but in not same position. I dont know why but my code always gives 1 for right and 3 for wrongplace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{ 
    int j;
    int right=0;
    int wrongplace = 0;
    int random[4] = {1,2,3,4};        
    int comparing[40] = {3,2,1,3};
    int line = 3;
    comparing[9] = 1;
    comparing [10] = 2;
    comparing [11] = 3;
    comparing [12] = 4;
          
     for (int m =1 ; m <= 4; m++)
    {
         j  = 4*(line-1)+m;
        cout << "j: " << j << endl;
        for (int k =0; k < 4; k++)
        {            
            if (comparing[j] == random[k]) 
            {
                wrongplace++;
                k =5;
            }
        }        
    }
    for (int z =0; z < 4 ; z++)
    {
        if (comparing[j] == random[z] )
        {
           right++;
        }
    }
    wrongplace = wrongplace-right;
    cout << right << "  "<< wrongplace;        
}

why do you put K=5 on line 23? why not using break?

I dont know why but my code always gives 1 for right and 3 for wrongplace

well that is how you initialize it here:
1
2
int random[4] = {1,2,3,4};        
int comparing[40] = {3,2,1,3};

1 is OK 3 is not OK.

why do expect to have output different that this?
I guess line 23 is used to exit the loop? Use break; instead.

Also how do you check that it's in the right place or not? You don't check indexes of your arrays.

You should check each element of the first with each element of the other and if you find a match just compare their indexes. That way if it matches you have a rightplace otherwise you have a wrongplace.

Also you don't initialize comparing besides the first 4 elements and 9-13. Don't do that. I think it's platform dependent if it's being initialized but never assume that. Force initialization yourself.
the code was only created here. I did not give my actual code because I might have a lot of unnecessary stuff lol

So,
I taking input of 4 numbers from the user 10 times. line represents line.
I want to reply to the user howmany he has got right and in ecxact position and how many he has got right but in wrong position.

I expect 1 for right and 2 for wrongplace.

btw now.
the first time it gives 0,0 and all other times 2, 2; i dont know what it is.
Topic archived. No new replies allowed.