tbwttihs
Mechanical
- Aug 4, 2003
- 20
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 << " "; //print out the contents to screen.
}
getche();
}
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 << " "; //print out the contents to screen.
}
getche();
}