Generating lottery numbers

This is a discussion on Generating lottery numbers within the C++ Programming forums, part of the General Programming Boards category; Hello Everybody. I have another problem that I do not know how to solve. Here is the problem: Write a ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    14

    Generating lottery numbers

    Hello Everybody.
    I have another problem that I do not know how to solve. Here is the problem:


    Write a function named generateLotteryNumbers. The function is passed an int array of size 5. The function should generate 5 different lottery numbers in the range 1 to 50 inclusive and place the numbers in the array. The declaration is as follows:
    void generateLotteryNumbers (int lotteryNumbers []);
    Note that no data is passed in to the function. The array is used to return the function results. Thus the parameter is an OUT parameter. Do not display the result. Return the result.
    Do not seed the random number generator inside the function. If you seed the random number generator inside the function and the function is called many times in the same second, your function will return the same results each time it is called.


    I know how to generate the numbers in the specified range but I do not know how to test for duplicates. Here is the code I have so far:


    Code:
    //This program will test the "generateLotteryNumbers" function
    
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void generateLotteryNumbers (int lotteryNumbers[]);
    
    
    void main()
    {
    	int lotteryNumbers[5];
    	srand (time(0));
    
    
    	generateLotteryNumbers (lotteryNumbers);
    
    
    	cout << lotteryNumbers << endl;
    }
    
    
    
    
    //This function will generate five lottery numbers
    #include <iostream>
    using namespace std;
    
    
    void generateLotteryNumbers (int lotteryNumbers[])
    {
    	const int SIZE = 5;
    	int randomNumbers;
    	int index = 0;
    	int number;
    	bool used;
    
    
    	for (int index = 0; index < SIZE;  index++) 
    	{
    		int number = rand()% 50 + 1;
    		do
    		{
    			used = false;
    			for(int index = 0; index < SIZE;  index++) 
    			{
    				
    				if( number == randomNumbers[index] )
    					used = true;
    				
    			}
    		}while(used);
    		randomNumbers[index] = number;
    	}
    }

    when I try to compile this, my compiler tells me that lines 41 and 46 require an array or pointer type.
    I have no idea what that means and my instructor will not tell me. He just says that there is something wrong.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    What is your input parameter called, and why aren't you using it?

    Having two nested loops, both using the same variable (index) is very confusing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    14
    Hello Salem:

    The input parameter is ( int lotteryNumbers[] ), I was trying to get the validation of the generated numbers figured out. As for not using the input parameter, I am not sure how to write that, I am having trouble understanding arrays, and passing them back and forth between functions.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    If you are having that many problems with arrays maybe you should visit the tutorial on this site.

    Arrays in C and C++ - Cprogramming.com

    After that you can read the section on pointers

    Pointers in C and C++ - Tutorial - Cprogramming.com

    This one might be hard for you to understand, but its all important for your task at hand

    Passing arrays to and from functions saf - C++ Forum

    If you don't get your mind wrapped around the data types you are using then you are going to have a real had time getting the programs to behave like you want them to do. There is no shame in making a simpler program first and once it works adding to it. You said you have a problem telling if a number is already in an array you could write a shorter program to just achieve that task. Once it is finished then you can add another level of functionality.

    Some of the programs I've done have neared 15k lines of code which become really fun to manage coding on your own. If I didn't test each function seperately before throwing the into the big program I would have crashes every other day. Its alot easier to track an error in 10 lines of code than it is to find in 10k lines of code.

    As for your code itself look back at your do while loop and the value of used. If you find a matching number in the array one time you will get lost in that loop forever.
    Last edited by Lesshardtofind; 12-04-2012 at 11:54 PM.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    counters i and j and k is a standard convention for nested loops if you dont have anything more meaningful they need to be called
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    14

    validating the generated numbers

    I now have the generator working,the next problem is to validate the numbers generated to insure there are no duplications. Here is the generator code that works:

    Code:
    void generateLotteryNumbers(int lotteryNumbers[])
    {
      const int arraySize = 5;
      int index = 0;
      
      while (index < arraySize)
      {
        lotteryNumbers[index] = rand() % 50 + 1;
        cout << lotteryNumbers[index] << endl;
        index++;
      }
    }
    Can someone please help me with this.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well you had something approaching a workable approach in post #1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    14
    I found that online but did not understand what it was doing.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    That is why it's not a good idea to copy and paste code. If you write it yourself, you will (in theory) know exactly what it is doing.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    14
    Hello Matticus:
    At the time I thought I could do a little research and figure it out. Do you have any useful suggestions?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Suggestions - yes. Close the IDE/text editor, take out a pen and paper, and try to develop an algorithm by hand. When you get an idea of how to solve the problem, this will help you structure your code accordingly. This is preferable to asking "how do I do this?" in a forum each time you get stuck. I'm not trying to be rude - this is genuine advice that will help improve your programming skills.

    As to suggestions for your particular question (checking for duplicates):

    There are a few ways to go about this. As a beginner, it would be best if you took a "one step at a time" approach - the algorithm won't be as efficient as it could be, but it will work when done right. Here is a suggested algorithm for you to review:

    Step 1: Fill the array with random numbers
    Step 2: Sort the array (bubble sort is a common sorting algorithm that I find beginner-friendly)
    Step 3: Check for duplicates - now that the array has been sorted, checking for duplicates is straightforward. Basically, if value[i] == value[i+1], then you have a duplicate. If you use this method, be sure to structure your loop terminating condition carefully so you don't overrun your array! (Hint: look at the 'i+1')
    Step 4: If a duplicate is found, generate a new random number for that position

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    14
    Hello Matticus:

    I don't mean to be a pain but I am in a first year class and we haven't covered how to sort an array or anything else for that matter. You seem to have a great deal of knowledge on this that I haven"t been exposed to yet. Your patience would be greatly appreciated.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by les2012 View Post
    Code:
    void generateLotteryNumbers(int lotteryNumbers[])
    you cannot pass arrays in this way!

    you need to do one of the following:

    Code:
    template<int n>
    void function(int (&arrayParameter)[n])
    or

    Code:
    void function(int *arrayParameter, size_t n)

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by Elkvis View Post
    you cannot pass arrays in this way!

    you need to do one of the following:

    Code:
    template<int n>
    void function(int (&arrayParameter)[n])
    or

    Code:
    void function(int *arrayParameter, size_t n)
    I agree with the sprit of this. The best alternative is generally:
    Code:
    void function(LotteryNumbersClass arrayParameter);
    As another variant on your code, one could do:
    Code:
    const unsigned LOTTO_SIZE = 5;
    
    void function(int (&arrayParameter)[LOTTO_SIZE])
    Last edited by King Mir; 12-05-2012 at 07:03 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating odd numbers from set
    By student0806 in forum C Programming
    Replies: 2
    Last Post: 09-27-2010, 04:36 PM
  2. Generating Random Numbers
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2009, 07:01 AM
  3. Need Help Generating Random Numbers In C++
    By slickwilly440 in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2005, 01:38 PM
  4. help with generating numbers
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 08-06-2003, 08:29 AM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21