The assignment was to write a program that generated a random sequence of six non-repeating numbers between 1 and 53 until it matched a user inputted sequence (lotto).
Here is the entire program.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cctype>
#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
main ()
{
char loop;
int ticket[6];
int lotto[6];
int a;
int b;
int c;
int d;
do
{
c = 0;
for(b = 0; b <= 5; b++)
{
ticket[b] = 0;
}
do
{
system("CLS");
d = 0;
printf("Please enter your lotto numbers.\n");
for(b = 0; b <= 5; b++)
{
scanf("%i", &ticket[b]);
}
for(b = 0; b <= 5; b++)
{
if (ticket[b] == 0)
{
d = 1;
printf("There must be 6 non-zero, non-negative lotto numbers. Try again.\n");
system("Pause");
break;
}
}
for(b = 0; b <= 5; b++)
{
if (ticket[b] > 53)
{
d = 1;
printf("Lotto numbers must be between 1 and 53. Try again.\n");
system("Pause");
break;
}
}
for(b = 0; b <= 4; b++)
{
if (ticket[b] == ticket[b + 1])
{
d = 1;
printf("Lotto numbers cannot repeat. Try again.\n");
system("Pause");
break;
}
}
} while(d == 1);
do
{
a = 0;
for(b = 0; b <= 5; b++)
{
srand ( time(NULL) );
lotto[b] = rand() % 53 + 1;
}
for(b = 0; b <= 5; b++)
{
if(ticket[b] != lotto[b])
{
a = 1;
}
}
c++;
cout << c <<endl;
} while (a == 1);
printf("Your odds are 1 to %i\n", c);
printf("Would you like to to waste your money again? Y/N.\n");
printf(" No, seriously, you should open a savings account.\n");
cin >> loop;
loop = tolower(loop);
} while(loop == 'y');
}
The problem doesn't occur until the comparison between the user's sequence and the generated sequence.
do
{
a = 0;
for(b = 0; b <= 5; b++)
{
srand ( time(NULL) );
lotto[b] = rand() % 53 + 1;
}
for(b = 0; b <= 5; b++)
{
if(ticket[b] != lotto[b])
{
a = 1;
}
}
c++;
cout << c <<endl;
} while (a == 1);
Is it not possible to compare arrays this way? What happens is the loop continues without stop, I realize it should take some time but 6.5 million iterations is unreasonable. Any suggestions?