How would you read and compare to arrays

How would you compare to arrays and determine if the containers of both of the arrays are the same? So here is an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ostream>
#include <fstream>
#include <vector>
using namespace std;
string printthis("The Lottery numbers are");
string printlost("Sorry you didn't win");
string printwin ("You won a million dollars");

using namespace std;
int main(){
int lottery[5] = {45,22,11,4,9},user[5];
string print("Please enter 5 numbers");
cout << print << endl;

for (int i =0; i < 5; i++ ){

    cin >> user[i];

}

}
How would you compare everything in user and everything in lottery[5]?
Something like this?

1
2
3
4
5
6
7
8
9
int match;
match = 0;
for (int i =0; i < 5; i++ )
{
    if (lottery[i] != user[i])
        match = 1;
}
if (match == 1)
    cout << "arrays don't match every item"
You have included vector, use it?

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
33
34
35
36
37
38
39
40
std::vector<int> lottery, user;

lottery.push_back(45);
lottery.push_back(22);
lottery.push_back(11);
lottery.push_back(4);
lottery.push_back(9);

// Vectors can have numerous types
// So lets create a simple generic function
template<typename T>
bool Contains(const std::vector<T>& v1, T val)
{
        // Loop through contents
        for(size_t i = 0; i < v1.size(); i++)
            if(v1[i] == val) return true; // if element contains value return true
	return false; // If execution survives loop the value does not exist in vector
}

while(user.size() < lottery.size()) // Makes it a little more scalable rather than magic numbers
{
	std::cout << "Please enter a number: ";
	int temp;
	std::cin >> temp;
	// Best to check input but omitted for clarity
	
	if(Contains(user, temp)) // Check if number already used - because temp is int I believe compiler can resolve template ( Don't worry about that too much )
            std::cout << "You already chose that number!\n\n";
	else
            user.push_back(temp); // Add it to user choices
    // Keep repeating till user has picked 5 numbers
}

// sort the vectors from lowest to highest
// NOTE - Must include <algorithm>
std::sort(lottery.begin(), lottery.end());
std::sort(user.begin(), user.end());
// Check for equality
if(user == lottery) std::cout "You won!\n\n";
else std::cout << "You lost!\n\n";


EDIT: An extension to CodeWriters approach

1
2
3
4
5
6
7
8
9
10
11
12
13
bool Equal(int& array1, int& array2, size_t elements)
{
	for(size_t i = 0; i < elements; i++) // loop through arrays
            // if elements don't match they are not equal
	    if(array1[i] != array2[i]) return false;
        // If execution survives the loop the arrays are equal
        return true;
}

int lottery[5], user[5];

if(Equal(lottery,user,5)) std::cout << "You won!\n\n";
else std::cout << "You lost!\n\n";


EDIT 3:

Fixed minor bug in vector version

EDIT 4:

Forgot to sort vectors ( Sorry it's half 2 in the morning :)
Last edited on
Topic archived. No new replies allowed.