An array comparing another array.

Nov 22, 2008 at 8:17pm
Hi there, I would like to ask about an array of any comparing another array of the same type. Lets take an int array for an example, the first one comparing with the second one, they have the same number but different position and i would like to print "Same number diff position, try again !". Or sth similar to it. Can anyone show me an sample code? Thanks.
Nov 22, 2008 at 8:27pm
You could use nested for-loops: loop for every element from array A trough all the elements of array B and compare them.
Nov 22, 2008 at 8:49pm
Yeah, but i can only do the same position and same number part. I want erm there is the same number in the array but different position
Nov 22, 2008 at 8:55pm
1
2
3
4
5
6
7
8
9
10
11
int firstArray[10];
int secondArray[10];

for (int i=0;i<=9;i++){
   for (int j=0;j<=9;j++){
      if (firstArray[i]==secondArray[j]){
         if (i==j) //do what you want to do if there're on the same position
         else //do what you want to do if there're on different positions
      }
   }
}
Nov 22, 2008 at 9:24pm
hmmm there is another small problem. When the number is repetitive, for example 2122, and i input 2122, it does the if and also the else statement.
Nov 23, 2008 at 8:11am
That isnt hard to avoid... Create an array of bool values, or chance the order in my above code. I'm sure there are much more solutions.
Last edited on Nov 23, 2008 at 8:11am
Nov 23, 2008 at 4:42pm
ok anyway thanks.
Topic archived. No new replies allowed.