CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    7

    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
    Join Date
    May 2009
    Posts
    2,413

    Re: Generating lottery numbers

    Quote Originally Posted by Les2012 View Post
    my compiler tells me that lines 41 and 46 require an array or pointer type.
    Your teacher may not tell you but the compiler did. Don't just use randomNumbers as an array, declare it as one too.

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Generating lottery numbers

    PL check declaration "int randomNumbers" ( int variable) and its use as " randomNumbers[index] " (array element)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Generating lottery numbers

    Two points.

    const int SIZE = 5;
    should be declared outside a function and used anywhere you need to know the size of the array. You declare the array in main using the number 5, then specify the loop boundary using the constant. You should be consistent.

    Second, another approach to the same problem is to populate an array with the numbers 1 - 50 than call random_shuffle to randomize them. Return the first 5 elements of the shuffled array. You don't have to worry about dupes that way.

  5. #5
    Join Date
    Dec 2012
    Posts
    7

    Re: Generating lottery numbers

    In the instructions it tells me to generate the numbers and to validate that there are no duplicates. Also, I am not allowed to use global variables. But thank you for the suggestion I will remember it for later use.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Generating lottery numbers

    Quote Originally Posted by Les2012 View Post
    In the instructions it tells me to generate the numbers and to validate that there are no duplicates. Also, I am not allowed to use global variables. But thank you for the suggestion I will remember it for later use.
    The way you use SIZE in your code would be regarded as bad technique. I doubt when the instructor said don't use global variables they meant don't use a const int like that. An alternative would be to say
    #define SIZE 5
    before you start writing functions too.

    That accomplishes the same thing.

    I don't think my suggestion violates the parameter of the assignment. Your function would return 5 random numbers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured