P: n/a

hi, i have an assignment to put 6 pseudo random numbers into an array to
simulate drawing 6 lottery numbers between 149. 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 pseudorandom 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();
}  
Share this Question
P: n/a

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***************@newsbinary.blueyonder.co.uk... hi, i have an assignment to put 6 pseudo random numbers into an array to simulate drawing 6 lottery numbers between 149. 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 pseudorandom 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(); }  
P: n/a

moi wrote: hi, i have an assignment to put 6 pseudo random numbers into an array to simulate drawing 6 lottery numbers between 149. 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 pseudorandom 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  
P: n/a

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 pseudorandom 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  
P: n/a

"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 reseeding the RNG and this might become a
pitfall sometime later!
Regards
Chris  
P: n/a

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 reseeding 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  
P: n/a

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 shortcomings
:P Good luck with your studies!
James
__________________________________
A random quote of nothing:
Fatal Error: Found [MSWindows] System > Repartitioning Disk for Linux...
(By cb*****@io.org, Christopher Browne)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 9559
 replies: 6
 date asked: Jul 19 '05
