×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

help me im a rookie....

help me im a rookie....

help me im a rookie....

(OP)
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();
}


RE: help me im a rookie....

For starters, change void main(int lottery_numbers[5]) to void main(). The only parameters that the main function will take are command line parameters. I don't see where you declare the array anywhere (you may want to do this inside your main function). Your code talks about 6 lottery numbers but your array will only hold 5 (array[5] will have 5 elements numbered 0-4).

I hope this gets you on the right track.

RE: help me im a rookie....

also look into the rand function

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources