write a program that simulates a lottery. the program should have an array of fi
ID: 3638075 • Letter: W
Question
write a program that simulates a lottery. the program should have an array of five integers named winningdigits, with a randomly generated number in the range 0-9 for each element in array. the program should ask the user to enter 5 digits and should store them in a second arrray named plaed. the program must compare the corresponding elements in the two arrays and count how many digits match, once the user has entered a set of numbers the program should display the winning digits and the players digits and tell how many digits match.Explanation / Answer
please rate - thanks
you didn't say anything about whether to allow duplicate numbers of not, so I didn't
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
bool NoDuplicates(int[],int,int);
void GenWinNums(int[],int);
void getLottoPicks(int[],int);
int checkwins(int[],int[],int);
void results(int[],int[],int,int);
int main()
{srand(time(0));
int plaed[5],winningdigits[5],max=5;
int usercount=0,winningcount=0,i,matches=0;
getLottoPicks(plaed,max);
GenWinNums(winningdigits,max);
matches=checkwins(plaed,winningdigits,max);
results(plaed,winningdigits,max,matches);
system("pause");
return 0;
}
void results(int a[],int b[],int n,int m)
{int i;
cout<<"YOUR PICKS ";
for(i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<"WINNING TICKET NUMBERS: ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"Number Matches "<<m;
cout<<endl<<endl;
return;
}
int checkwins(int a[],int b[],int n)
{int i,j,same=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]==b[j])
{same++;
}
return same;
}
void GenWinNums(int a[],int n)
{int i;
bool dup=true;
for(i=0;i<n;i++)
{while(dup)
{
a[i]=rand()%9+1;
dup=NoDuplicates(a,i,2);
}
dup=true;
}
}
void getLottoPicks(int a[],int n)
{int i;
bool dup=true;
for(i=0;i<n;i++)
{while(dup)
{do
{cout<<"Enter number (between 1 and 9) "<<i+1<<": ";
cin>>a[i];
if(a[i]<1||a[i]>9)
{cout<<"number out of range ";
}
}while(a[i]<1||a[i]>9);
dup=NoDuplicates(a,i,1);
}
dup=true;
}
return;
}
bool NoDuplicates(int a[],int n, int f)
{int i,j;
bool dup=false;
if(n==0)
return false;
for(i=0;i<=n-1;i++)
if(a[n]==a[i])
{dup=true;
if(f==1)
{cout<<"DUPLICATE PICK numbers you've already chosen are:";
for(j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
}
}
return dup;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.