I have to make a program that simulates a lottery. The program should have an array of 5 digits named winningdigits, a randomly generated number in the range of 0 through 9 for each element in the array. The program should ask the user to enter 5 digits and should store them in a second integer array named player. The program must compare corresponding elements in two arrrays and count how many digits match.

This is an example: There are two matching digits, elements 2 and 4.
winningDigits: 7, 4, 9, 1, 3
player: 4, 2, 9, 7, 3
Print out the matches and how much money the user made.
Money made = Match(es) X 100
Example:
2 Matches = $200

So far I have this, I'm not sure how to make them match. Do I do the same type of thing for player like I did for winningDigits? I'm not really sure what to do after this?

#include <iostream>
using namespace std;


int main()
{
    int winningDigits[5];
    int Matches;
    int i; 
    int a[10];

    for (int i = 0; i<5; i++)
        winningDigits[i] = rand() % 10;

    cout << a[10] << endl; 

    Money made = Matches  X 100


    system("pause");
    return 0;
}

Recommended Answers

All 6 Replies

I assume the array of winning digits can not contain duplicate numbers -- for example this would be illegal: 7 1 4 7 2 5 because the number 7 appears more than once. If that is correct, then you need to expand the loop on line 12 to check the array to see if the value returned by line 13 already exists in the array.

line 15: a[10] accesses one element beyond the end of the array, that is, there is no 10th element. Remember, arrays always start numbering with 0, so the last elment in that array is a[9].

Why does arry a contain 10 elements when only the first 5 are used?

You need to add something before line 15 to get user input for 5 digits. Again, you will probably want to check for duplicates.

It probably doesn't matter if the digits in the two arrays are in the same order. So you might use a 3d array to check off the digits that appear in both arrays. You could use a bool array for that.

bool checks[5] = {false};

Now loop through both the first two arrays. If the first value in the second array (user inputs) appears anywhere in the first array (winningDigits) then set checks[0] to true. Do that for each of the other digits in the user inputs array. When done, all you have to do is count the number of true values in the checks array.

Once you get all that done the rest should be fairly easy to print out.

Don't attempt to program all this at the same time. Do a little bit, compile, correct errors then repeat.

Hey, what the problem is asking you is to count the total number of times an element exists inside an array.

Assume the following:

A = [1, 2, 3, 4]
B = [2, 1, 2, 4]

Then you can do the following:

AϵB

This is going to either give you true or false..

Because 2 in B exists in A, 1 exists... Therefore, only having one array does not make sense, there has to be two arrays, one containing the winning results and another containing the user's results. Apply this, therefore:

pseudocode

int count = 0; 
foreach(element in A)
{
   int c = element;
   if(c == element)
   {
      counts++; 
   }
}

Sorry, I could not give you the answer. But, have a go and let me know if you struggle with anything

I'm still not really getting it, I sort of understand what you guys are trying to say but I'm not sure of how to write it.

 #include <iostream>
using namespace std;
int main()
{
    int winningDigits[5];
    int players;
    int Matches;
    int i; 
    int a[9];
    for (int i = 0; i<5; i++)
        winningDigits[i] = rand() % 10;
    cout << a[9] << endl; 
    for (int i = 0; i<5; i++)
        players[i] = rand() % 10; 
    Money made = Matches  X 100
    system("pause");
    return 0;
}

I don't get how to make them match with each other.

No, you would just create a rand for the users, you would input the user:

int player[5]; 

for(int i=0; (i < 5); i++)
{
   std::cout << "Please enter number" << i;
   cin >> player[i];
}

then you would carry out the comparision.

Hope this helps

I don't get how to make them match with each other.

You will need two loops, one inside the other. The outer loop will step through each of the digits in winningDigits array. The inner loop will attempt to find the current digitit in winningDigits in the player array. If a match is found then put that digit in a third array so that you keep track of the matching digits. Something like this:

for each digit in winningDigits array
{
   for each digit in player array
   {
       if current digit in player array is the same as winningDigits array
       {
           add the digit to matches array

       }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.