473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

lottery number thing problem

moi
hi, i have an assignment to put 6 pseudo random numbers into an array to
simulate drawing 6 lottery numbers between 1-49. the code i have to do this
so far is listed below. i dont think its far off. im getting a recurring
error regarding the syntax of my main() function argument calling in
lottery_numbers[] array. so any help as to whats glaringly going wrong
here?? oh and some brief guidance on whats the easiest way to sort the
numbers into ascending order in the array before i cout << them.

p.s. i am aware from some replies to earlier posts that some of the things
like the includes' etc is a bit archaic but for the purposes of this
assignment, i dont really need it pointing out. cheers.

here's the code as it stands....

// lottery.cpp
// generates six pseudo-random numbers
// and displays them in ascending order.

#include<iostream.h> //c++ I/O
#include<stdlib.h> //standard function library
#include<conio.h> //getche()
#include<time.h> //time(), time_t

bool checknum(int num, int lottery_numbers[5])
{
for (int x=0; x <= 5; x++)
{
//check number is not equal to any other number already in array
//and is greater than zero.
if (lottery_numbers[x] == num || lottery_numbers[x] == 0)
{
return false; //the number is invalid. it is rejected.
}
}
return true; //the number is valid. it is accepted
}
void DrawNumbers(int lottery_numbers[5])
{
int num_count = 0; //counter for number of valid numbers found.

while (num_count <=5) //i.e. while all 6 numbers have not yet been drawn.
{
//initialise random number generator
time_t t;
srand(unsigned (time(&t)));
int num = (rand()*49); //generate a random number between 0 and 49
if (checknum(num, lottery_numbers)) //use checknum() to test validity
{
lottery_numbers[num_count] = num; //set current array element to
validated num.
num_count++; // move on to next element in array to fill.
}
}
}

void main(int lottery_numbers[5])
{
DrawNumbers(lottery_numbers[]); //'draw' the numbers into the array
for (int i = 0; i <=5; i++) // for each of the six elements of the array...
{
cout << lottery_numbers[i] << " "; //print out the contents to screen.
}
getche();
}


Jul 19 '05 #1
6 10238
moi
oops, i realise it should be lottery_numbres[6]....just a typo...

doh, i spotted a few more mistakes too, which ive corrected below. initial
question still stands though...

"moi" <ra****************@blueyonder.co.uk> wrote in message
news:bn***************@news-binary.blueyonder.co.uk...
hi, i have an assignment to put 6 pseudo random numbers into an array to
simulate drawing 6 lottery numbers between 1-49. the code i have to do this so far is listed below. i dont think its far off. im getting a recurring
error regarding the syntax of my main() function argument calling in
lottery_numbers[] array. so any help as to whats glaringly going wrong
here?? oh and some brief guidance on whats the easiest way to sort the
numbers into ascending order in the array before i cout << them.

p.s. i am aware from some replies to earlier posts that some of the things
like the includes' etc is a bit archaic but for the purposes of this
assignment, i dont really need it pointing out. cheers.

here's the code as it stands....

// lottery.cpp
// generates six pseudo-random numbers
// and displays them in ascending order.

#include<iostream.h> //c++ I/O
#include<stdlib.h> //standard function library
#include<conio.h> //getche()
#include<time.h> //time(), time_t

bool checknum(int num, int lottery_numbers[])
{
for (int x=0; x <= 5; x++)
{
//check number is not equal to any other number already in array
//and is greater than zero.
if (lottery_numbers[x] == num || lottery_numbers[x] == 0)
{
return false; //the number is invalid. it is rejected.
}
}
return true; //the number is valid. it is accepted
}
void DrawNumbers(int lottery_numbers[])
{
int num_count = 0; //counter for number of valid numbers found.

while (num_count <=5) //i.e. while all 6 numbers have not yet been drawn.
{
//initialise random number generator
time_t t;
srand(unsigned (time(&t)));
int num = (rand()*49); //generate a random number between 0 and 49
if (checknum(num, lottery_numbers)) //use checknum() to test validity
{
lottery_numbers[num_count] = num; //set current array element to
validated num.
num_count++; // move on to next element in array to fill.
}
}
}

void main(int lottery_numbers[])
{
int lottery_numbers[5];
DrawNumbers(); //'draw' the numbers into the array
for (int i = 0; i <=5; i++) // for each of the six elements of the array... {
cout << lottery_numbers[i] << " "; //print out the contents to screen.
}
getche();
}

Jul 19 '05 #2


moi wrote:

hi, i have an assignment to put 6 pseudo random numbers into an array to
simulate drawing 6 lottery numbers between 1-49. the code i have to do this
so far is listed below. i dont think its far off. im getting a recurring
error regarding the syntax of my main() function argument calling in
lottery_numbers[] array. void main(int lottery_numbers[5])
The signature of main is always (well, not always, but until you get
more experienced ...)
either:

int main()

or

int main( int argc, char* argv[] )

The reason: main is called from the *operating system*. And the
operating system wants to pass either nothing or some command line
arguments. But why and how should the operating system know that
your main wants some lottery_numbers?
You want

int main()
{
int lottery_numbers[6];

Also: Throughout your program you make one mistake in array
dimensioning. You need 6 numbers, thus your array must have
a size of 6, not 5!

int lottery_numbers[6];

You have confused this with *accessing* the array, where the
highest valid index equals 5. Valid indices in an array of size
6 are: 0, 1, 2, 3, 4, 5. Count them, they are exactly 6.

so any help as to whats glaringly going wrong
here?? oh and some brief guidance on whats the easiest way to sort the
numbers into ascending order in the array before i cout << them.

p.s. i am aware from some replies to earlier posts that some of the things
like the includes' etc is a bit archaic but for the purposes of this
assignment, i dont really need it pointing out. cheers.

here's the code as it stands....

// lottery.cpp
// generates six pseudo-random numbers
// and displays them in ascending order.

#include<iostream.h> //c++ I/O
#include<stdlib.h> //standard function library
#include<conio.h> //getche()
#include<time.h> //time(), time_t

bool checknum(int num, int lottery_numbers[5])
{
for (int x=0; x <= 5; x++)


Don't do that. The C or C++ idiom for a loop through
all the valid array indices is:

for( int x = 0; x < array_size; x++ )
So if you have an array of size 6, this becomes:

for (int x=0; x < 6; x++)

The reason for this is: It is an idiom. no C programmer needs to
think of the details. If one sees a loop like this

for ( some_var = 0; somevar < some_size; somevar++ )

in context with an array, everbody knows immediatly that this
generates all valid indices into the array of size some_size.
At the moment the comparison in the for loop is different, everybody
has to take a closer look, if it is correct.

I haven't looked to closely at the rest, if you have more problems
feel free to post.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
Hello,

"moi" <ra****************@blueyonder.co.uk> writes:
p.s. i am aware from some replies to earlier posts that some of the things
like the includes' etc is a bit archaic but for the purposes of this
assignment, i dont really need it pointing out. cheers.
So, if you are aware of that, why don't you change it? It looks a bit silly.
bool checknum(int num, int lottery_numbers[5])
{
for (int x=0; x <= 5; x++)
{
//check number is not equal to any other number already in array
//and is greater than zero.
if (lottery_numbers[x] == num || lottery_numbers[x] == 0)
Why test if the number ==0? Why not generate numbers >0 to begin with?
{
return false; //the number is invalid. it is rejected.
}
}
return true; //the number is valid. it is accepted
}
void DrawNumbers(int lottery_numbers[5])
{
int num_count = 0; //counter for number of valid numbers found. while (num_count <=5) //i.e. while all 6 numbers have not yet been drawn.
{
//initialise random number generator
time_t t;
srand(unsigned (time(&t)));
The srand should not be inside the loop. Move it out of it.
int num = (rand()*49); //generate a random number between 0 and 49
This generates all kinds of things, except what you want. Think about it.
The rand() function returns a pseudo-random integer between 0 and RAND_MAX.
How would you make an integer between >0 and <= 49 from that with (almost)
equal probability? Or, if you like, try to figure out how to make the
probabilities exactly equal.
void main(int lottery_numbers[5])
main should return an int. Furthermore why does it take in integer array
as an argument. What is wrong with int main()?
{


Declare your lottery_numbers inside main. Like
int lottery_numbers[5]={-1,-1,-1,-1,-1}; .
Initialize with -1 to prevent discarding numbers because some random
memory location happened to already contain a valid lottery number.

Bye,
Chris Dams
Jul 19 '05 #4

"Chris Dams" <ch****@gamow.sci.kun.nl> wrote in message
news:bo**********@gamow.sci.kun.nl...
Hello,

[SNIP]> > while (num_count <=5) //i.e. while all 6 numbers have not yet been
drawn.
{
//initialise random number generator
time_t t;
srand(unsigned (time(&t)));


The srand should not be inside the loop. Move it out of it.

[SNIP]

I'd even suggest to move it out of the whole function into main() 'cause
there is certainly no point in re-seeding the RNG and this might become a
pitfall sometime later!

Regards
Chris
Jul 19 '05 #5
Chris Theis wrote:
"Chris Dams" <ch****@gamow.sci.kun.nl> wrote in message
news:bo**********@gamow.sci.kun.nl...
Hello,


[SNIP]> > while (num_count <=5) //i.e. while all 6 numbers have not yet been
drawn.
{
//initialise random number generator
time_t t;
srand(unsigned (time(&t)));


The srand should not be inside the loop. Move it out of it.


[SNIP]

I'd even suggest to move it out of the whole function into main() 'cause
there is certainly no point in re-seeding the RNG and this might become a
pitfall sometime later!

Regards
Chris

Hello,

I donīt know if I am leading the original poster to far away, but as
this surely is a c++ listing I would do this completely different:
- create a set, put the 49 Numbers into it
- do a random shuffle with the set
- get the first six numbers out of the set and put them in a vector
- sort the vector, cout the numbers

You will need to read the documentation to the following headers:
<set>
<vector>
<algorithm> - you need that for the random shuffle and for the sorting
(look for random_shuffle and sort).

If you want to do the actual sorting yourself you will need a sorting
algorithm. You can learn very much if you try to invent your own. How
would you sort playing cards? If you want to program this yourself you
should stick to the array.

Best regards, Andreas

Jul 19 '05 #6
moi wrote:
im getting a recurring
error regarding the syntax of my main() function argument calling in
lottery_numbers[] array. so any help as to whats glaringly going wrong
here?? oh and some brief guidance on whats the easiest way to sort the
numbers into ascending order in the array before i cout << them.
What error exactly? I'm not going to cut, paste, compile this either - an
error message would be useful (keep reading).

As for the sort, you can do a bubble sort in about 4 lines of code, or you
could use a vector and then leverage some of the STL magic for sorting etc.
Not sure if you're allowed to use the STL in your assignment tho :-/
p.s. i am aware from some replies to earlier posts that some of the things
like the includes' etc is a bit archaic but for the purposes of this
assignment, i dont really need it pointing out. cheers.
<PEDANT>
So *when* do you plan on writing your code correctly? Get into good habits
and it will serve you well. :) If you're aware of the "include" errors
below, then why not just fix them?
</PEDANT>
here's the code as it stands....
**Snipped - lets just look at "main"**
void main(int lottery_numbers[5])
oops. try:
int main(void)

Your "main" declaration is telling the compiler to expect integers passed
from the command line - not what you are wanting. Also "void main()" is
generally accepted as B.A.D. :)
{
initialise your array here like this:
int lottery_numbers[6];

Also, replace any "lottery_numbers[5]" with "lottery_numbers[6]". You want
SIX elements in your array, not 5. Sure the array is indexed from 0 to 5,
but you need to define your array properly otherwise your "for (int x=0; x
<= 5; x++)" will overflow the array; index 5 doesn't exist in your original
code.

Hint: I always define my arrays and loops like this:
int myArray[10]; // an array with 10 elements
for(int i=0; i<10; i++) // iterate through the array
{
myArray[i]=i*i; // store the square of the index at the index.
std::cout << myArray[i] << std::endl;
}

Notice "int myArray[MAX_SIZE]" and "for(...; i<MAX_SIZE;...)". Just keeps
your brain on the right track :) Notice I've used "i LESS THAN" not "<=".
DrawNumbers(lottery_numbers[]); //'draw' the numbers into the array
for (int i = 0; i <=5; i++) // for each of the six elements of the
array...
{
cout << lottery_numbers[i] << " "; //print out the contents to screen.
}
getche();
insert here:
return 0; // main returns an int now, so let's give it one :)

}


No doubt others far more skilled than I will point out my own short-comings
:P Good luck with your studies!

--James
__________________________________
A random quote of nothing:

Fatal Error: Found [MS-Windows] System -> Repartitioning Disk for Linux...
(By cb*****@io.org, Christopher Browne)

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: NTL Newsgroups | last post by:
Hi, I've written a lottery programme in PHP that selects 10 winners a week from approximately 18,000 shares. The problem I have is that of the ten winners one week the same share number was...
11
by: Kevin Williamson | last post by:
Hi, I've written a lottery programme in PHP that selects 10 winners a week from approximately 18,000 shares. The problem I have is that of the ten winners one week the same share number was...
3
by: dazza2122 | last post by:
I want to design a program to genarate lottery number the number have to be unique and be 0 to 49. Can anybody shed some sort of light on the code it would take
1
by: um33 | last post by:
Hello guys, I am knew to the VB6 and I do need to wrie a program that generate 6 lottery numbers in ascending Order and Non repeating as well. Thanks
8
by: Miktor | last post by:
I'm trying to write a lottery number generator for the uk national lottery. Any clues where I'm going wrong? #include <cstdlib> #include <iostream> using namespace std; int main ()
0
by: pokerislotto | last post by:
We invent a method called pokerization of lottery to play lottery strategically. This is the website : http://lottoispoker.bravehost.com/ We deal with UK National Lottery, Canada Lotto 6/49 and...
8
by: ImortalSorrow | last post by:
Hey, I'm quite new in programing so need some help with this lottery program I'm making. What I'm trying to do is very simple, simulate a lottery but my effords are in vain since when I'm compiling...
4
by: 1q2w3e | last post by:
You are to create a lottery game program that allows a user to simulate playing the lottery. In this lottery game, the winning number combination comprises four unique numbers, each in the range of...
0
by: | last post by:
UK NATIONAL LOTTERY DESK Reference No: ACCU/2007-200 Good day Winner No: A333/ZZ5, We use this medium to notify you of the lottery prize won by your email address, in this year's edition of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.