Comparing two arrays
Author |
Message |
DNA
|
Posted: Fri Jan 15, 2010 5:21 am Post subject: Comparing two arrays |
|
|
What is it you are trying to achieve?
For my ISP, I need to compare 2 arrays which both have a length of 4. They contain integers.
What I need to figure out is the amount of values in one array that match with both position and value with the other array, as well as the amount of values in one array that match only value with the other array.
My ISP is a game of MasterMind (board game).
The amount of matching (by position and by value) will be defined with a black counter. The amount of partially matching will be defined with a white counter.
For example; I have an array with the values of: 34, 25, 11, 67. I have another array with the values of 67, 34, 11, 95.
The output would be: 1 black counter (because 11 has the same position in both arrays), and 1 white counter (because 34 exists in both arrays, but in different positions)
What is the problem you are having?
I do not understand how to sort the arrays out properly to get the correct amount of counters. I have tried using for loops, but that just leads to the "alreadyCounted" array being out of bounds.
Describe what you have tried to solve this problem
I have tried using for loops, but that just leads to the "alreadyCounted" array being out of bounds.
Sorry, it may just be me being tired, but I really don't get it at this point.
Thank you!
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
var answer, guess : array 1 .. 4 of int
var alreadyCounted : array 1 .. 10 of int
var whiteCounter, checker, inverseChecker, counter, blackCounter : int := 0
counter := 1
answer (1) := 12
answer (2) := 31
answer (3) := 15
answer (4) := 12
guess (1) := 11
guess (2) := 15
guess (3) := 31
guess (4) := 12
%Supposed to be; 1 black, 2 white
for x : 1 .. 10
alreadyCounted (x ) := 0
end for
for x : 1 .. 4
for y : 1 .. 4
checker := 10 * x + y
inverseChecker := 10 * y + x
if answer (x ) = guess (y ) and x = y then
blackCounter := blackCounter + 1
alreadyCounted (counter ) := checker
counter := counter + 1
if answer (x ) = guess (y ) and (alreadyCounted (1) and alreadyCounted (2) and alreadyCounted (3) and alreadyCounted (4)
and alreadyCounted (5) and alreadyCounted (6) and alreadyCounted (7) and alreadyCounted (8) and alreadyCounted (9) and alreadyCounted (10)
) not= (checker and inverseChecker ) then
whiteCounter := whiteCounter + 1
alreadyCounted (counter ) := checker
alreadyCounted (counter + 1) := inverseChecker
counter := counter + 2
end if
end if
end for
end for
locate (10, 1)
put whiteCounter, " white counters."
put blackCounter, " black counters."
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Turing_Gamer
|
Posted: Fri Jan 15, 2010 8:47 am Post subject: Re: Comparing two arrays |
|
|
Well for comparing 2 arrays normally you can have a for loop that happens to be 1 .. 4 then have a second for loop 1 .. 10 in side that loop.
Then you can have a bunch of if statements.
I don't know if that's what you mean't because your code is confusing. |
|
|
|
|
|
TheGuardian001
|
Posted: Fri Jan 15, 2010 9:35 am Post subject: Re: Comparing two arrays |
|
|
Well, you can use nested for loops to check if it matches any values, and if you use nested for loops, you can check if they got the position right by comparing the for loops' counters, as in
code: |
for i : 1 .. 4
for j : 1 .. 4
if guess (i) is equal to answer (i)
they got a guess right (white counter)
%We know they matched the numbers, check if they
%matched the position.
if i equals j
they got the position right. (black counter)
end if
end if
end for
end for
|
If that's what you were already doing, sorry I couldn't be more help, but I also couldn't really read that code (gigantic if statement you got there) |
|
|
|
|
|
Turing_Gamer
|
Posted: Fri Jan 15, 2010 9:56 am Post subject: Re: Comparing two arrays |
|
|
You could have a for statement 1 .. 10 for that gigantic if statement and have a boolean to check the requirements.
Have an if statement after the for loop checking the boolean |
|
|
|
|
|
|
|