1

I am trying to do function which compare to arrays and returns true if they are same. Now the arrays are simple, it will be advanced later but I am stuck on the testEqual function. So here is code

int n = 5;
int array[5] = {5,10,3,4,7};
bubbleSort(pole,n);

int array2[5] = {3,4,5,7,10};
testEqual( array , array2 , "bubbleSort");

And here is testEqual function I need to remake on arrays but I don't know how.

bool testEqual(int i1, int i2, const string testName) {
    bool myresult = (i1 == i2);
    return myresult;
}

The other functions like bubbleSort are fine I just need to remake testEqual.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
user2466601
  • 207
  • 1
  • 7
  • 19
  • 2
    [`std::equal`](http://en.cppreference.com/w/cpp/algorithm/equal) (The array overloads for [`std::begin`](http://en.cppreference.com/w/cpp/iterator/begin) and [`std::end`](http://en.cppreference.com/w/cpp/iterator/end) will also be useful) – BoBTFish Jan 30 '14 at 13:48
  • 1
    What does qualify as "two arrays are equal"? –  Jan 30 '14 at 13:48
  • 1
    Your function tests just two numbers, not arrays. You should probably pass pointers to your arrays as function parameters, together with array size, and then run a loop over all elements inside the function. That should get you on the right track. – DUman Jan 30 '14 at 13:48
  • @user1264727, adding the remark that you should first test that the sizes are equal, I would upvote this as an answer especially given the lack of effort / understanding shown in the question. – CompuChip Jan 30 '14 at 13:51
  • Also see http://stackoverflow.com/questions/7273340/array-contents-equality-in-c – CompuChip Jan 30 '14 at 13:54
  • @CompuChip I specifically omitted that because the array size is hardcoded, and given that this is obviously a homework type of problem, that can be dismissed. Perhaps I am wrong not to mention it even in this case. – DUman Jan 30 '14 at 13:56

4 Answers4

7

Following may help:

template <typename T, std::size_t N>
bool isEqual(const T (&lhs)[N], const T (&rhs)[N])
{
    return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
}

If you use std::array, you have that for free. (And the syntax is more friendly).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

To compare two arrays you could use standard algorithm std::equal.

For example

bool testEqual( const int *first1, const int *last1, const int *first2, const int *last2 )
{
    return std::equal( first1, last1, first2, last2 );
}

It can be called the following way

testEqual( array, array + 5, array2, array2 + 5 );

As for your function then it is invalid.

It simply compares two integers and it is not clear what is the meaning of the third parameter

bool testEqual(int i1, int i2, const string testName) {
    bool myresult = (i1 == i2);
    return myresult;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

i see it the same as H2CO3 "what qualifies as equal?"

the approach with std::equal does not match to the arrays you are providing...std::equal will take same elements AND order.

I modified the example from cplusplus.com

int main () {
  int myints[] = {20,40,60,80,100};               
  int myints2[] = {20,100,60,40,100};               
  std::vector<int>myvector (myints2,myints2+5);     // myvector: 20 40 60 80 100

  // using default comparison:
  if ( std::equal (myvector.begin(), myvector.end(), myints) )
    std::cout << "The contents of both sequences are equal.\n";
  else
    std::cout << "The contents of both sequences differ.\n";

  return 0;
}

resulting in

The contents of both sequences differ.

so for using std::equal, you should sort them before

beardedN5rd
  • 185
  • 6
-1

You can also use std::equal. For example :

#include <algorithm>

int *ints;
ints = new int[10];

bool EqualArray(const Object& obj)
{
   return std::equal(ints,ints + 10, obj.ints);
}

Of course you can also overload the operator== for other things. Unfortunately you can't overload it for primitive arrays, because overloading operators is only allowed if at least one argument is a class (or struct) type. But you could override it to compare vector with array. Something like:

template<typename T, typename Alloc, size_t S>
bool operator==(std::vector<T, Alloc> v, const T (&a)[S])
{
    return v.size() == S && std::equal(v.begin(), v.end(), a);
}

(this takes reference to array not degraded to pointer to check it's declared size first and is therefore safe).

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105