Arrays

hey everyone im trying to write a program that generates 5 random numbers from the cpu and have the user input 5 numbers. depending on the user's input if the numbers match up the user wins 100$/number matched with the cpu.


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
#include <iostream>
#include <ctime>
using namespace std;

int main(){
    
    int winningDigits[5];
    int player[5];
    
    
    //randomize the winning numbers
    srand(time(0));
    
    //for loop for the winning numbers
    for (int i=0; i<5; ++i)
    {
        winningDigits[i] = rand()%10;
    }
    
    //print out the winning numbers
    for (int i=0; i<5; ++i)
    {
        cout<<winningDigits[i]<<" ";
    }
    
    //allow the player to enter 5 digits
    cout<<"enter 5 digits:";
    cin>> player[0];
    cin>> player[1];
    cin>> player[2];
    cin>> player[3];
    cin>> player[4];
    
    //disply player input
    cout<<" "<< player[0];
    cout<<" "<< player[1];
    cout<<" "<< player[2];
    cout<<" "<< player[3];
    cout<<" "<< player[4];
    
    system("pause");
    return 0;
}
}
Last edited on
Line 17: winningDigits[5] = rand()%10; The 5 should be i as in: winningDigits[i] = rand()%10;

If you are trying to compare them you may want to put lines 27->31 after lines 37->39 then check if they match with a simple if statement.
Actually a simple if statement might not do it unless the OP wants to compare the position as well.
i.e.

winningDigits = {1,2,3,4,5}
player = {5,4,3,2,1}
1 digit matched '3'


If you the OP want's to do something kinda like the lottery where order doesn't matter then I'd suggest a sort on both arrays and then compare for equal and less than...

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
void sort(int size; int* arr){
    int temp = 0;
    bool repeat = false;
    while(repeat){
        for(int i=0; i<size-1; i++){
            if(arr[i+1] < arr[i]){
                temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
                repeat = true;
            }
        }
    }
}

int compare(int size, int* ar1, int* ar2){
    int matched = 0;
    sort(size, ar1);
    sort(size, ar2);
    for(int i=0, j=0; i<size && j<size;){
        if(ar2[j] == ar1[i]){
            matched++;
            i++;
        }
        else if(ar2[j] < ar1[i])
            j++;
        else
            i++;
    }
    return matched;
}

winningDigite = {1,2,3,4,5}
player = {5,4,3,2,1}
5 digits matched


I think that'd work. =)
Last edited on
position does matter in my particular case but i just do not know how to format the loop for the program to match both the player and the random winning numbers if that makes sense. thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool equal = true;
for(int i = 0; i < numbers; ++i)
{
    if(winningDigits[i] != players[i])
    {
        equal = false;
        break; //exit the loop could replace this
        //if you do equal = winningsDigits[i] == players[i]
       //then instead of i < numbers you put i < numbers && equal
    }
}


if(equal)
{
    std::cout << "The two arrays are the same!" << std::endl;
}
else
{
    std::cout << "The arrays are not the same!" << std::endl;
}


*edit fixed code tags
Last edited on
But still you'd require a sort first as he said the order doesn't matter, a magnitude sort will place all values in the same position (if they match that is)

Once doing the sort you can then either just check if they all match with your code, just iterating through and checking if each element is equal... OR you can actually check how many of them are equal with the functions I wrote, which is useful for things like lottery where you have larger prizes for matching more numbers.
that is the most up to date code that i have but it does not display anything into the compiler. any ideas why?
1
2
3
4
5
6
7
8
9
10
11
    //for loop for the winning numbers
    for (int i=0; i<5; ++i)
    {
        winningDigits[i] = rand()%10;
    }
    
    //print out the winning numbers
    for (int i=0; i<5; ++i)
    {
        cout<<winningDigits[i]<<" ";
    }
Why not output after getting the number?

1
2
3
4
5
6
7
8
//I would avoid the magic numbers (5 & 10)
//give them a good name like numberOfDigits
//and maxValue/upperLimit
for(int i = 0; i < 5; ++i)
{
    winningDigits[i] = rand() % 10;
    cout << winningDigits[i] << ' ';
}


You also have a few things that can be easily done with loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    //allow the player to enter 5 digits
    cout<<"enter 5 digits:";
    cin>> player[0];
    cin>> player[1];
    cin>> player[2];
    cin>> player[3];
    cin>> player[4];
    
    //disply player input
    cout<<" "<< player[0];
    cout<<" "<< player[1];
    cout<<" "<< player[2];
    cout<<" "<< player[3];
    cout<<" "<< player[4];
could just as easily been written as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Enter 5 digits: ";
for(int i = 0; i < numberOfDigits; ++i) //mentioned earlier to avoid magic numbers like 5
{
    cin >> player[i];
}

//or you could even have done:
//cin >> player[0] >> player[1] >> player[2] >> player[3] >> player[4];

cout << "Player digits: ";
for(int i = 0; i < numberOfDigits; ++i)
{
    cout << player[i] << ' ';
}
Topic archived. No new replies allowed.