EDIT I've seen a few wondering what I am asking for. I haven't written the algorithm for it yet, but after the program runs and outputs the 2 arrays, I need to output how many numbers matched. There is an example of that below. My apologies, I really thought that I made it as clear as possible. Here is the example again. If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index. I need to write an algorithm for this, though I have not yet. I'm sure I need a function with a for loop. I'm looking for any tips on how to go about it.
I've seen many iterations of this program on StackOverflow but none have been able to help me out so here I am asking for help.
I have this program that has 2 arrays. In one array that generates 5 random numbers from 0-9, the name is winningDigits
. The other array (player) is for 5 numbers the user inputs. The goal of the user is to input 5 numbers and hope they all match with the randomly generated numbers.
I am almost done with the program, my only issue is trying to output how many digits were matched. For digits to match, they must be in the same spot in the player array as the winningDigits
array.
For example If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
int player[5];
int size = 5;
int count = 0;
cout << "Welcome to Lottery Simulator." << endl << endl;
cout << "Enter 5 numbers ranging from 0-9 one at a time, enter -1 when finished." << endl;
// Loop for user input
cout << "Enter number: ";
cin >> player[count];
while (player[count] != -1)
{
count++;
cout << "Enter number: ";
cin >> player[count];
}
cout << endl;
cout << "Winning Numbers User Numbers" << endl;
cout << "--------------- ------------" << endl;
// Generating random winning numbers
srand(time(0));
int winningDigits[size];
for (int i = 0; i < size; i++) {
winningDigits[i]= rand() % 10;
cout << setw(8) << winningDigits[i] << setw(17) << player[i] << endl;
}
return 0;
}
Now I'm thinking I'll need to make a function with a for loop, but so far I'm stuck. Just in case it wasn't clear enough, I am looking to output how many digits were matched. I hope my example from above will help you all understand what I'm looking for. Sorry, this is only my second post here and I am trying to make everything as clear as possible. Please ask if you need any information or help in understanding what I wrote.