3

I wrote this code:

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        int[] tal1 = { 3, 2, 3};
        int[] tal2 = { 1, 2, 3};

        Console.WriteLine(t.theSameInBoth(tal1,tal2));
    }
}

class Test
{
    public Boolean theSameInBoth(int[] a, int[] b)
    {
        bool check = false;

        if (a.Length == b.Length)
        {
            for (int i = 0; i < a.Length; i++)
                if (a[i].Equals(b[i]))
                {
                    check = true;
                    return check;
                }
        }

        return check;
    }
}

So the deal here is. I need to send in two arrays with numbers in them. I then need to check the arrays through. If ALL the numbers in the array are identical. I need to set my check as true and return it. The only problem is. With the code i set here, where i sent an array with 3,2,3 and one with 1,2,3 it still return check as true.

I'm a newbie at this, so i hoped anyone in here could help me!

Kris Vandermotten
  • 10,111
  • 38
  • 49
  • http://msdn.microsoft.com/en-us/library/vstudio/bb348567(v=vs.100).aspx – liran63 Oct 26 '14 at 17:58
  • It's a code assignment for school. The problem is i can't use that. I've already tried it, and worked like a charm. But sadly i can't use it. – Nickolai Schmidt Oct 26 '14 at 17:59
  • http://stackoverflow.com/questions/649444/testing-equality-of-arrays-in-c-sharp – Sybren Oct 26 '14 at 18:00
  • Oh, be fair with the downvotes, folks. Honestly... ;-) – Craig Tullis Oct 26 '14 at 18:00
  • LINQ is slick and everything, but has anybody run a performance test to see how it compares to running the comparison with imperative code in a good old-fashioned loop? Also, if this is a school assignment, presumably intended as an exercise in learning how to program, versus being an exercise in learning Microsoft's LINQ syntax, then the imperative code approach might be more appropriate. – Craig Tullis Oct 26 '14 at 18:04

4 Answers4

3

You need to reverse the tests:

class Test
{
    public bool theSameInBoth(int[] a, int[] b)
    {
        if (a.Length == b.Length)
        {
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i])
                {
                    return false;
                }
            }

            return true;
        }

        return false;
    }
}

As soon as one pair of items is different, the two arrays are different.

In your code, you effectively said that as soon as one pair of items are equal, the two arrays are equal.

Kris Vandermotten
  • 10,111
  • 38
  • 49
1

you are returning as soon as you find the first match. You need something like this:

bool check = true;

if (a.Length == b.Length)
{
   for (int i = 0; i < a.Length; i++)
   {
      if (!a[i].Equals(b[i]))
      {
            check = false;
            break;
       }

   }
   return check;
}
else 
  return false;
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1
bool isIndentical = true;

if (a.Length == b.Length)
{
    for (int i = 0; i < a.Length; i++)
         if (!a[i].Equals(b[i]))
         {
            isIndentical = false;
            return check;
         }
}

return isIndetical;

Try it like this. Your code always return true, because if one of the element of the arrays are equal the code in your if statement will return true. In my case I'm checking for not equal and if this happen just return false. See that I use other variable which makes this more clear and make it true in the begging.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

My habit is to add the Linq solution:

public bool IsSame(int[] a, int[] b)
{
    return a.Length == b.Length &&
           a.Select((num, index) => num == b[index]).All(b => b);
}

Another cool Linq approach:

public bool IsSame(int[] a, int[] b)
{
    return a.Length == b.Length &&
           Enumerable.Range(0, a.Length).All(i => a[i] == b[i]);
}
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
  • Ill have to look into LINQ. Havent looked at it all! So basically what you wrote, does the same as all i wrote? Seems like the easier solution. – Nickolai Schmidt Oct 26 '14 at 18:16
  • Broken down, it looks a little different, but logically it does the same thing, yes. Efficiently speaking, simple loops is better, but very insignificantly. You should go with whatever you're more comfortable with reading and understanding, which would probably not be Linq. Again, I just love Linq. – SimpleVar Oct 26 '14 at 18:36
  • 2
    There is already such a function in linq: `a.SequenceEqual(b)` – Magnus Oct 26 '14 at 18:53
  • Yeah, I've already tried the SequenceEqual, and it seemed way simpler, but as i said, but it's a school assignment, and we were told not to use it – Nickolai Schmidt Oct 26 '14 at 19:15
  • @NickolaiSchmidt So what exactly is the assignment? – Magnus Oct 26 '14 at 20:30
  • @Magnus It's just simple code assigments to improve your coding skills. But here is the assigment. Inside the class Test, there is placed a public methods sumOfAllNumbers. The method receives an int Array and answer with a number. The method shall take every number in the Array and answer with the Sum. Ex: The method receive {2,3,1,5}. It answer 11. Ex: The method receive {2,7}. It answer 9. – Nickolai Schmidt Oct 27 '14 at 09:03
  • It's fun how most assignments can be solved with one line of Linq (`return arr.Sum();`) but you're not allowed to use it :P – SimpleVar Oct 27 '14 at 15:49