Check array for equality

closed account (EwCjE3v7)
I have this excercise
Write a program to compare two arrays for equality, write a similar one with vectors

What does it mean by equality? Size? Value of elements?
Probably both size and values should be equal to operand to be equal.
Bonus: program for vectors
1
2
3
4
5
std::vector<int> x;
std::vector<int> y;
//Fill vectors
if (x == y)
    std::cout << "equal";
Yes and yes. Arrays A and B are equal if A[k] == B[k] for all k, and obviously {3, 2} is not equal to {3, 2, 7}
closed account (EwCjE3v7)
Okay once on PC I will upload the code and to see if it is right.

Edit: 1 problem. Here is my code, I have only done the equality for the elements and will do greater than or less than after

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iterator>
#include <vector>
using std::cout; using std::endl; using std::cin;
using std::begin; using std::end;
using std::vector;

int main()
{
    int arr[] = {1,2,3,4,5};
    int arr2[] = {1,3,3,4,5};
    if (end(arr) - begin(arr) == end(arr2) - begin(arr2)) {
        cout << "The first array has the same amount of elements as the second one." << endl;

        unsigned counter = 0;

        while (counter != end(arr) - begin(arr) && arr[counter] == arr2[counter]) {
            ++counter;
            if (counter == end(arr) - begin(arr)) {
                cout << "Elements are the same to." << endl;
            }

        }
    }
    else if (end(arr) - begin(arr) > end(arr2) - begin(arr2)) {
        cout << "The first array is larger than the second" << endl;
    }
    else if (end(arr) - begin(arr) < end(arr2) - begin(arr2)) {
        cout << "The second array is larger than the first" << endl;
    }
    return 0;
}


PROBLEM
This was my first code, I don`t understand the commented line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iterator>
#include <vector>
using std::cout; using std::endl; using std::cin;
using std::begin; using std::end;
using std::vector;

int main()
{
    int arr[] = {1,2,3,4,5};
    int arr2[] = {1,2,3,4,5};
    if (end(arr) - begin(arr) == end(arr2) - begin(arr2)) {
        cout << "The first array has the same amount of elements as the second one." << endl;

        unsigned counter = 0;
        
        while (counter != end(arr) - begin(arr) && arr[counter] == arr2[counter]) {
            if (counter == (end(arr) - begin(arr)) - 1) { // so if 4 of the 5 elements are equal?
                cout << "Elements are the same to." << endl;
            }
            ++counter;
        }
    }
    else if (end(arr) - begin(arr) > end(arr2) - begin(arr2)) {
        cout << "The first array is larger than the second" << endl;
    }
    else if (end(arr) - begin(arr) < end(arr2) - begin(arr2)) {
        cout << "The second array is larger than the first" << endl;
    }
    return 0;
}


Edit again, okay after breaking my head I have got it. Thank you
Last edited on
Topic archived. No new replies allowed.