0

Create a method named ArrayCompare that compares between two arrays and returns a boolean indicating whether they contain exactly the same elements or not. Its input should be two integer arrays a and b. Its output should be a boolean, where it will return true if a and b contain the same elements and false otherwise

Hello, I am new to c#, and I would like to know how this is done. I read up some documents and I can only come up with this.

    static bool ArrayCompare(int [] i , int [] j)
    {
        if (i.Length == j.Length)
        {
            for (int o = 0; o < i.Length; o++)
            {

                //I am stuck here
                return false;
            }
        }
        return false;
    }
4
  • You can compare i[o] to j[o] at this place and check if they are identical. Commented Oct 8, 2021 at 10:44
  • I am not sure how to compare the arrays Commented Oct 8, 2021 at 10:45
  • i[o] and j[o] are just integers and can be complared using == or != Commented Oct 8, 2021 at 10:46
  • Does the order matter? Commented Oct 8, 2021 at 10:53

4 Answers 4

4
 static bool ArrayCompare(int [] i , int [] j)
    {
        if (i.Length == j.Length)
        {
            for (int o = 0; o < i.Length; o++)
            {
                if(i[o]!=j[o])
                   return false;
            }
            return true;
        }
        return false;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

You never return true
@KlausGütter my bad i edited
Note: alternatively you can also use the IEnumerable extension method SequenceEqual, but in this case I feel that the homework need the algorithmic implementation to be provided
1

You can check using the code blow

int[] array1 = { 1, 2, 2 };
int[] array2 = { 1, 2, 2 };

bool check = array1.SequenceEqual(array2);

2 Comments

while this does indeed work, I feel like the homework assignment is meant to have him write his own algorithm, instead of using an extension method
@MihaiC the question does not mention if this is an assignment or not. And unless otherwise specified, it is usually a good idea to use builtin functions when possible.
0

This should work!

static bool ArrayCompare(uint [] a, uint [] b)
{
    bool equal = true;
    if(a.Length== b.Length)
    {
        for(int i = 0,j=0; i < a.Length; ++i,++j)
        {
            if (a[i] != b[j])
            {
                   equal = false;
                   break;
           }
        }
    }
    return equal;
}

Comments

-3
    static bool ArrayCompare(int[]a, int [] b)
    {

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

        return false;

    }

This method takes in two int arrays as parameters, we are then checking to first make sure that both the arrays are the same length before we loop. In the for loop we are checking to make sure that if the current index of the first array is the same as the current index of the second array is the same. If it is, we get the to last index of both arrays, and if the same we return true, if the indexes are not the same we return false. We can then create two arrays and console out the method with the two arrays as parameters.

4 Comments

So, {1, 2, 3} and {1, 4, 3 } would be considered identical? I'm not sure what this is trying to do, but I'm fairly sure it is incorrect.
@JonasH My bad, will fix my code while I wait for your answer
this is wrong. it will return true even if the arrays are not equal
@MihaiC Yeah I know that, as already pointed out, no need to say again? I have realized my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.