Okay, so my assignment was to "create a program that will generate up to 5 quick pick Lotto Tickets. You must ask the user how many tickets they want and then randomly pick 6 numbers for each ticket between 1 and 50. Remember no ticket can have the same two values on it (i.e. 3, 5 22,34, 34, 50). For 10 bonus points output the tickets in ascending order. The user can buy as many sets of 5 tickets as they would like one set at a time.
//Library Section
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <apmatrix.h>
usingnamespace std;
//const section
//var section
int Ticket;
char Ans;
int I;
int J;
vector<int>Numbers(5);
void main()
{
srand(time(NULL));
J=0;
do{
cout<<"How many lottery tickets would you like? Please enter a number up to 5:"<<endl;
cin>>Ticket;
for(Ticket=0;Ticket<Numbers[I];Ticket++)
{
for(I=0;I<Numbers.size();I++)
Numbers[I]=(rand()%50)+1;
for(I=0;I<Numbers.size();I++)
cout<<"Lottery ticket:"<<Numbers[I]<<endl;
}
cout<<"Run program again (Y/N)?"<<endl;
cin>>Ans;
}while(Ans=='y'||Ans=='Y');
}//end main
What I still need help on is with the code to create the numbers, stop the program from creating duplicates, and the ascending order part would be nice :)
As of right now, the code does not even run correctly! Help would be much appreciated.
The generating of the random numbers along with the prevent duplicates goes hand in hand. Check this logic: make a dynamic array (eg std::vector)
while you have less numbers than you need
make a random number
check to see if you already have the number (eg with std::find)
if you did not find the number, add it
Use srand(time(0)) at the start of main and never again, then use rand() to get a random number.
For the bonus, you can use std::sort on your std::vector (or even simple array) and it will sort the array for you ;)
//var section
vector<int>Ticket(5);
char Ans;
int First;
int Second;
int Third;
vector<int>Numbers(6);
int N;
int T;
bool I;
int F;
void main()
{
srand(time(NULL));
First=0;
Second=0;
Third=0;
do{ //main do while loop
system("cls");
do{ //Do while for tickets
cout<<"How many lottery tickets would you like? Please enter a number up to 5:"<<endl;
cin>>N;
if(N<1||N>5)
cout<<"Please enter a valid number of tickets."<<endl;
}while(N<1||N>5);
for(F=0;F<N;F++) //Main for loop
{
for(Third=0;Third<6;Third++) //Create randomness
{
Numbers[Third]=((rand()%50)+1);
do{ //duplicate prevention
I=false;
for(Second=0;Second<Third;Second++)
{
if(Numbers[Second]==Numbers[First])
{
I=true;
Numbers[First]=((rand()%50)+1);
}
}
}while(I==true);
}
for(First=0;First<Numbers.size();First++)
{
for(Second=First+1;Second<Numbers.size();Second++)
{ //least to greatest
if(Ticket[First]>=Ticket[Second])
{
T=Ticket[First];
Ticket[First]=Ticket[Second];
Ticket[Second]=T;
}
}
}//ends the least to greatest
for(Third=0;Third<Numbers.size();Third++) //Displays the numbers
{
cout<<Numbers[Third]<<" ";
}
cout<<endl;
}
cout<<"Run program again (Y/N)?"<<endl;
cin>>Ans;
}while(Ans=='y'||Ans=='Y');
}//end main
I made a powerball program that do the numbers. No duplicates. I tried to copy the whole program in here wit the quicksor, printout, time stamp files and the whole nine yards but was to big to fit. If you want it, send me an e-mail and I will send you the file. perhojfeldt@gmail.com. Works great by the way. Have won a couple of times. Pure luck. Ha Ha. Here is the randomizing part of it with no duplicates generated.
void CreateRandom(void)
{
/* Setup a random seed number from computer time */
srand(time(NULL));
/* Get random number */
do {
uiSame = 0;
/* Fill buffer with random numbers between 1 and 59 */
for (uiCnt = 0; uiCnt < 5; uiCnt++) {
uiBufNumber[uiCnt] = (1 + rand()) % 60;
}
/* Test for any numbers are the same. Redo if numbers are the same. */
for (uiCntA = 4; uiCntA > 0; uiCntA--) {
for (uiCnt = 0; uiCnt < uiCntA; uiCnt++) {
if (uiBufNumber[uiCnt] == uiBufNumber[uiCntA]) {
uiSame = 1;
}
}
}
} while (uiSame != 0);
}
I don't know why it don't do the indent. They are all spaces. Weird.
Here is another way to do the random number. Do it like they do on TV.
#1. Make a random number (RN) between 1 and 50.
Save it in bin 1 (B1).
Do #1.
#2. Compare RN with B1.
If different save in bin 2 (B2)
else do #1 and goto #2.
and so on.
The different with what I did in the code is I did all random numbers at once and checked of any was the same and if they were did it all over again until they are all different. This will this longer time (ms) because the chance of getting 2 numbers alike out of 5 is higher than 2 numbers out of 2. Hopes this help.
I wrote the code and checked it. works fine. Create random number between 1 and 50 where none are the same. Only thing missing is the quick sort and that it. Her you go.
void CreateRandom_1(void)
{
int iPointer;
int iRandomNumber[5];
/* Setup a random seed number from computer time */