S
sam
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide
your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:
/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NMAX 6
int a[NMAX];
int b[NMAX];
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray(int x[]);
void winnings();
int wins = 0;
int main(void) {
//welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPlease Enter Six Whole Numbers Between 1 And 49\n\n");
getIntArray(a);
getrandomarray(b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);
winnings();
return 0;
}
void getIntArray(int x[])
{
int n = 0;
int temp;
here://I know goto loops are not liked in C, but this is the best way to
achieve this feature in my opinion!
;
do {
printf("Choose A Number\n");
scanf("%d", &temp);
if (temp==0) break;
if (n==5)
{
printf("All Six Numbers Have Been Entered, Press Zero To Confirm\n");
}
if ((temp<01) || (temp>49))
{
printf("%s\n", "Sorry, That Is An Unacceptable Number. Please Re Enter");
goto here;
}
if (temp==a[0])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[1])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[2])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[3])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[4])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[5])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
else
a[n++] = temp;
}while (1);
}
void bubbleSort(int x[])
/* It sorts in non-decreasing order the six positions of a. It uses
* a technique called the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;
while (limit) {
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
if (x[lcv]>x[lcv+1]) {
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
void printArrays(int x[], int y[])
/* These values are printed out, six per line. */
{
//print user array
for (int i=0; i<NMAX
{
printf("\t%d ", x[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");
printf("The Drawn Numbers In Numerical Order Are: \n");
//print random array
for (i=0; i<NMAX{
printf("\t%d ", y[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");
}
void getrandomarray(int x[])
{
//make random numbers
srand((unsigned int)time((time_t *)NULL));
for (int i=0; i<NMAX; i++)
{
x = (rand()%49)+1; //This safety feature makes the
generator regenerate should any
for(int j=0; j<i; j++) //of its numbers be the same as previous
numbers.
{
if(x == x[j])
i--;
}
}
}
void bubblesort(int x[])
/* It sorts in non-decreasing order the first 6 positions of b. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;
while (limit)
{
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
{
/* Notice that the values in positions LIMIT+1 .. in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1])
{
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
}
void winnings()
{
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a==b[j])
{
wins++;
}
}
if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}
}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n");
return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations, You Have Won
£5,000,000!!\n");
return;
}
}
ok-so i guess its a mess to most of you, it compiles and runs no bother,
just doesnt give a correct readout at the end of how many numbers have
matched! iv looed at this for 4 days now, seems to me that only one or no
numbers are being matched almost as if its doing a true and false as to
whether any numbers match. trouble is, the lottery rules are 0, 1 or 2
numbers matched = £0, 3 =£10, 4=£500, 5=£300,000, 6= £5,000,000. any quick
fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard
to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?
thanks in advance
sam
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide
your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:
/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NMAX 6
int a[NMAX];
int b[NMAX];
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray(int x[]);
void winnings();
int wins = 0;
int main(void) {
//welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPlease Enter Six Whole Numbers Between 1 And 49\n\n");
getIntArray(a);
getrandomarray(b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);
winnings();
return 0;
}
void getIntArray(int x[])
{
int n = 0;
int temp;
here://I know goto loops are not liked in C, but this is the best way to
achieve this feature in my opinion!
;
do {
printf("Choose A Number\n");
scanf("%d", &temp);
if (temp==0) break;
if (n==5)
{
printf("All Six Numbers Have Been Entered, Press Zero To Confirm\n");
}
if ((temp<01) || (temp>49))
{
printf("%s\n", "Sorry, That Is An Unacceptable Number. Please Re Enter");
goto here;
}
if (temp==a[0])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[1])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[2])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[3])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[4])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[5])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
else
a[n++] = temp;
}while (1);
}
void bubbleSort(int x[])
/* It sorts in non-decreasing order the six positions of a. It uses
* a technique called the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;
while (limit) {
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
if (x[lcv]>x[lcv+1]) {
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
void printArrays(int x[], int y[])
/* These values are printed out, six per line. */
{
//print user array
for (int i=0; i<NMAX
{
printf("\t%d ", x[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");
printf("The Drawn Numbers In Numerical Order Are: \n");
//print random array
for (i=0; i<NMAX{
printf("\t%d ", y[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");
}
void getrandomarray(int x[])
{
//make random numbers
srand((unsigned int)time((time_t *)NULL));
for (int i=0; i<NMAX; i++)
{
x = (rand()%49)+1; //This safety feature makes the
generator regenerate should any
for(int j=0; j<i; j++) //of its numbers be the same as previous
numbers.
{
if(x == x[j])
i--;
}
}
}
void bubblesort(int x[])
/* It sorts in non-decreasing order the first 6 positions of b. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;
while (limit)
{
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
{
/* Notice that the values in positions LIMIT+1 .. in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1])
{
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
}
void winnings()
{
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a==b[j])
{
wins++;
}
}
if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}
}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n");
return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations, You Have Won
£5,000,000!!\n");
return;
}
}
ok-so i guess its a mess to most of you, it compiles and runs no bother,
just doesnt give a correct readout at the end of how many numbers have
matched! iv looed at this for 4 days now, seems to me that only one or no
numbers are being matched almost as if its doing a true and false as to
whether any numbers match. trouble is, the lottery rules are 0, 1 or 2
numbers matched = £0, 3 =£10, 4=£500, 5=£300,000, 6= £5,000,000. any quick
fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard
to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?
thanks in advance
sam