Struggling with comparing arrays

So, here is my program (C++). In it, I have created the first array (winningValues) to be assigned 5 random numbers between 0 and 1. The second array consists of numbers that the user has input. Assuming the arrays are correctly set up (I think they are), how would I go about printing which numbers match? For example, if array 1 consisted of 1, 2, 3, 4, 5, and array 2 consisted of 1, 2, 3, 5, 0, the output would print 1, 2, and 3 to the screen because they were the same number in the same location.

Any help would be greatly appreciated!

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include<iostream>

#include<ctime>

#include<cstdlib>

using namespace std;


int main()

{


const int MAXIMUM_VALUE = 9;

const int MINIMUM_VALUE = 0;

unsigned int seed;

int i;

seed = time(0);

srand(seed);

int count = 0;

int winningValues = 5; //constant to store array of 5 elements.... [0], [1], [2], [3], [4]

int win[winningValues] = {(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE, // winningValues[0]

(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE, // winningValues [1]

(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,

(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,

(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE};


cout << "Your lottery numbers are:" << endl;

for (int count = 0; count < winningValues; count++)

cout << win[count] << endl;


int playerValues [5];

int userInput;


for(int count = 0; count < 5; count++)

{

cout << "Please enter a number between 0 and 9: " << endl;

cin >> userInput;


while (userInput < 0 || userInput > 9)

{

cout << "Please enter a number between 0 and 9: " << endl;

cin >> userInput;

}


playerValues[count] = userInput;

}



}
closed account (SECMoG1T)
here is a simple example:

1
2
3
4
5
6
7
8
9
10
11
const int SIZE=6;

   int array1[SIZE]{1,2,4,4,5,8};
   int array2[SIZE]{1,2,3,4,5,6};

   ///the following elements are equal and at the same index in two diff arrays.
   for(int i = 0; i < SIZE; i++)
   {
       if(array1[i] == array2[i])///equality operator
        std::cout<<array1[i]<<" ";///print either because both are equal.
   }
Last edited on
So I revised it and got this, but it's only printing out one number at the end.... I'm sorry, this is my first language learning at university and I am utterly lost at what's going on here...

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
41
42
43
44
45
46
47
48
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{

    const int MAXIMUM_VALUE = 9;
    const int MINIMUM_VALUE = 0;
    unsigned int seed;
    seed = time(0);
    srand(seed);
    int count = 0;

    const int SIZE = 5;
    int winningValues[SIZE]{(rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
                            (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
                            (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
                            (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
                            (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE};

    int playerValues [5];
    int userInput;

    for(int count = 0; count < 5; count++)
    {
        cout << "Please enter a number between 0 and 9: " << endl;
        cin >> userInput;

    while (userInput < 0 || userInput > 9)
    {
        cout << "Please enter a number between 0 and 9: " << endl;
        cin >> userInput;
    }

    playerValues[count] = userInput;
    }

    for (int i = 0; i < SIZE; i++)
    {
        if (winningValues[i] == playerValues[i])
            cout << winningValues[i] << " ";
    }

}

 
Try this (run it several times):

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<ctime>
#include<cstdlib>

int main()
{

    const int MAXIMUM_VALUE = 9;
    const int MINIMUM_VALUE = 0;
    const int SIZE = 5;

    std::srand( std::time(nullptr) ) ;

    const int winningValues[SIZE]
    {
        (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
        (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
        (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
        (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE,
        (rand() % (MAXIMUM_VALUE - MINIMUM_VALUE + 1)) + MINIMUM_VALUE
    };

    int playerValues[5];

    for( int count = 0; count < 5; ++count )
    {
        int userInput = -1 ;

        while ( userInput < 0 || userInput > 9 )
        {
            std::cout << "Please enter a number between 0 and 9: " ;
            std::cin >> userInput;
        }

        playerValues[count] = userInput;
    }

    std::cout << "\n-----------\n\n" ;

    std::cout << "winning values: " ;
    for( int i = 0; i < SIZE; ++i ) std::cout << winningValues[i] << ' ' ;
    std::cout << '\n' ;

    std::cout << " player values: " ;
    for( int i = 0; i < SIZE; ++i ) std::cout << playerValues[i] << ' ' ;
    std::cout << '\n' ;

    int num_matches = 0 ;
    std::cout << "matched values: " ;
    for( int i = 0; i < SIZE; ++i )
    {
        if( playerValues[i] == winningValues[i] )
        {
            std::cout << playerValues[i] << ' ' ;
            ++num_matches ;
        }
        else std::cout << "  " ;
    }
    std::cout << "\n\nthere are " << num_matches << " matches\n" ;
}
Topic archived. No new replies allowed.