Passing an array to main:

This is a discussion on Passing an array to main: within the C++ Programming forums, part of the General Programming Boards category; I just want to send 6 random numbers to main from the random Generator function. I know I how to ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487

    Passing an array to main:

    I just want to send 6 random numbers to main from the random Generator function. I know I how to print them in the function and just simply call the function in main but I need the numbers.

    I am making a lottery game. Guess 6 numbers and if you get all 6 6 you win whatever and if you guess 2 you win something too. In the end it will loop until the computer matches the guessed lotto picks. Just showing how hard it is to guess correctly and win.


    Code:
     #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int random_Generator(int randNum[])
    {
    
        srand((unsigned)time(NULL));
        
        for (int i=0; i<6; i++) {
            randNum[i] = rand() % 49;
            
        }
        
    }
    
    int main(int argc, const char * argv[])
    {
        int numbers[7];
    
        for (int i=0; i<6; i++) {
            
        numbers[i]=random_Generator(numbers);
       
        cout<<numbers[i]<<endl;
            
        }
        return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your random_Generator should be void since it does not return any value - pay attention to compiler warnings

    You should call it outside the for loop
    It will fill the array.

    Inside the loop - just print the numbers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    It seems to me that you want your random_Generator function to populate the array. If so, then vart's suggestion is the way to go. But, then your random_Generator function should also take an argument specifying the size of the array. You should not use the magic number 6, and in fact it seems that you declared the array in main as having 7, not 6, elements.

    By the way, you should call srand near the start of the main function, not from within random_Generator. Otherwise, you will end up calling srand again should you call random_Generator more than once.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    I cannot wait till I stop making these dumb mistakes.

    A lightbulb just went off when you mentioned the compiler warnings. Sometimes I do not know what they mean, to only find out later it was literally giving my the answer to my question.

    C++ starts tomorrow, hope it severs me good!

    Thanks for the help...

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    @lazerlight, good suggestion. Makes it easier to read too.
    If I am suppose to output random numbers, which I am. Why is the first number always 30?

    Code:
     #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    #define SIZE 6
    
    using namespace std;
    
    void random_Generator(int randNum[],int size)
    {
    
        for (int i=0; i<SIZE; i++) {
            randNum[i] = rand() % 49;
            //cout<<randNum[i]<<endl;
        }
        
    }
    
    int main(int argc, const char * argv[])
    {
        srand((unsigned)time(NULL));
        
        int numbers[7];
        
        
        for (int i=0; i<SIZE; i++) {
            
        random_Generator(numbers,SIZE);
       
        cout<<numbers[i]<<endl;
            
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Ah, it is good to see that you used a named constant, SIZE. However, you forgot to use SIZE to declare the numbers variable in main. Furthermore, you should not use it in random_Generator, but rather use the size parameter. Oh, and then you're still calling random_Generator in the loop in main instead of just once before the loop.

    By the way, you should indent your code that is in the body of the loop in main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    I know where the problem is it just confusing. Please see the comment.

    Code:
     #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    #define SIZE 6
    
    using namespace std;
    
    void random_Generator(int randNum[],int size)
    {
        for (int i=0; i<SIZE; i++) {
            randNum[i] = rand() % 49;
        }
    }
    void compareLottoNum(int lottoNum[])
    {
            for (int i=0; i<SIZE; i++) {
            if (lottoNum[i]== random_Generator() { /*I know this is wrong because I need to values to pass 
                                                    to random_Generator. But that will mess up what I am trying to do. I want to compare the two. I have a feeling this fun project is going to teach me more then I thought.*/
                cout<<"You won 1,000,000,000 Euro!!!"<<endl;
            }
        }
        
    
    }
    
    
    int main(int argc, const char * argv[])
    {
        srand((unsigned)time(NULL));
        
        int numbers[SIZE];
        int lottoNumbers[SIZE];
        
        random_Generator(numbers,SIZE);
        
        cout<<"Enter 6 lotto numbers"<<endl;
        
        for (int i=0; i<6; i++) {
            cin>>lottoNumbers[i];
        }
        
        compareLottoNum(lottoNumbers);
        
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    Code:
      compareLottoNum(random_Generator(numbers,SIZE));
    This is pretty cool. I didn't know I could do this!

    I will figure this out tomorrow. I have to go to bed.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    This is pretty cool. I didn't know I could do this!
    Yeah, the way that works is that you call the one function before the other.
    Code:
    /* It doesn't matter what these do. */
    int f(void);
    int g(int);
    void h(int, int);
    
    g(f());
    h(g( 42 ), f());
    On line six we know that f() will be called before g(), so the value that f() returns will be the argument of the g() function. However, in the case of h(), f() or g() could be called first in the parameter list. If your functions are meant to be called in a specific order, then you should be more verbose.
    Code:
    int fReturned = f();
    int gReturned = g( 42 );
    h(gReturned, fReturned);
    Now there is no question about what order in which f() and g() were called.

    You use a return value from random_Generator(), but that function returns void. Passing in lottoNumbers was correct.
    Code:
      compareLottoNum(random_Generator(numbers,SIZE)); /* This is undefined behavior */
    Last edited by whiteflags; 09-01-2013 at 07:29 PM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your random_Generator ignores second parameter - it should be used to control the loop instead of SIZE
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    I almost got it but main has three errors. It was fine until I made the switch statement.
    The errors are:
    Code:
    Parse Issue
           Expected '(' for function-style case or type construction
    Parse Issue
           Expected ';' at end of declaration
    Parse Issue
           Expected '}'
    
    How when I add ; where it wants and add two more } int lottoNumbers still has a warning in Main. Also what I find weird is that the } after return 0 now has to have };
    Code:
     #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    #define SIZE 6
    
    using namespace std;
    
    void random_Generator(int randNum[])
    {
        srand((unsigned)time(NULL));        //creates a seed for rand()
        
        for (int i=0; i<SIZE; i++) {
            randNum[i] = rand() % 49;       //produces random number 0-49
        }
    }
    void compareLottoNum(int lottoNum[])    // lottoNum[] gets the user picked lotto numbers
    {
        int numbers[SIZE];              // will be used to store the random numbers in this function
        int count = 0;
        random_Generator(numbers);  /*random numbers are called and below compared against the users    numbers*/
        
        for (int i=0; i<SIZE; i++){
            
                if (lottoNum[i]==numbers[0]||numbers[1]||numbers[3]||numbers[4]||numbers[5]) {
                     count++;
                }
                if (count<=1) {
                    cout<<"You lost, maybe nexted time"<<endl;
            }
            
            switch (count) {
                case 2:
                    cout<<"You got two numbers right, you won 5 Euro"<<endl;
                    break;
                case 3:
                    cout<<"You got three numbers right you won 20 Euro"<<endl;
                    break;
                case 4:
                    cout<<"You got four right, you won 2,000 Euro"<<endl;
                case 5:
                    cout<<"You have won 100,000 Euro"<<endl;
                case 6:
                    cout<<"JACKPOT!!\n You won 1,000,000,000 Euro!!!"<<endl;
                    break;
                default:
                    break;
            }
        
    
    int main(int argc, const char * argv[])
    {
        
        int lottoNumbers[SIZE];
        
        cout<<"Enter 6 lotto numbers"<<endl;
        
        for (int i=0; i<6; i++) {
            cin>>lottoNumbers[i];
        }
     
        compareLottoNum(lottoNumbers);
    
        return 0;
    
    }

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Your 25 will not do you you intend it to do...

    So you have to describe your intentions first...

    I suppose you want to sort both number and lotto array and compare the entries with matching indexes
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    Yes I wanted to compare them and if they were equal count++.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The problem is you can have numbers 6,5,4,3,2,1 while user enters 1,2,3,4,5,6 - so easier way would be sort numbers...

    Also - your generator function doe snot check that numbers are not repeated.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    487
    Ahh ok I didn't know Last on first on applied to arrays. I thought since I was using the for statement everything would be called in order. Am I misunderstanding you?

    Code:
     if (lottoNum[i]== numbers[5]||numbers[4]||numbers[3]||numbers[2]||numbers[1]||numbers[0]) {
                     count++;
                }
    This is what I did to fix it, or at least I think it did. I am stilling getting the same errors. C++ started today so I suppose I should start digging in the text book more. I'll keep at this program and figure it out hopefully.

    Once I get the program to work I'll add some logic so it doesn't check twice for the same thing. It will also tell the user how many times the computer had to generate random numbers until they got 2,3,4,5,6 number correct. Just showing someone how hard it really is to win the lottery. Maybe I'll compare it to the Permutation of the lottery.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing array back to main function
    By greg677 in forum C Programming
    Replies: 11
    Last Post: 05-01-2010, 05:27 PM
  2. Passing info to main
    By Suchy in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2006, 11:15 PM
  3. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  4. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 04:59 AM
  5. passing a file name to main()
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-06-2001, 05:08 PM

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