Thread: Arrays making a lotto programme

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    Question Arrays making a lotto programme

    this is an array question we were asked to fiddle with and figure out in our spare time he said he will give the solution monday but I cant wait because ive been struggling with arrays and want to try and understand this if any one can help me please.
    Given the following 3 array definitions:
    int lotto7 = 3, 19, 4, 20, 21, 41, 26;
    int lotto_plus1 7 = 13, 11, 14, 33, 21, 36, 17;
    int lotto_plus2 7 = 40, 17, 14, 21, 35, 32, 30;


    You are required to write a program to ask the user to enter in their 6 lotto
    numbers.
    The program is to check the results for each of the 3 arrays above. The 7th number is the bonus number in each of the arrays above.
    Check the user numbers against each of the lotto arrays to see if the users has won any prizes:
    Winning combinations:
    - Match all 6 numbers with user numbers – wins jackpot
    -
    Match any 5 numbers with any 5 user numbers – wins cash prize
    -
    Match any 4 numbers with any 4 user numbers – wins cash prize
    -
    Match any 5 numbers and the bonus number with 6 user numbers – wins cash prize
    -
    Match any 4 numbers and the bonus number with any 5 user numbers – wins cash prize
    -
    Match any 3 numbers and bonus number with any 4 user numbers – wins a cash prize
    - Match any 3 numbers wins lottery scratch card
    - None of the above - not a winner

    The program is to display the results of each lotto array check to the user.

  2. #2
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    the rule is: You post the code you've already written then people will help you.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    
     int lotto[7] = {3, 19, 4, 20, 21, 41, 6};
     int lotto_plus1[7] = {13, 11, 14, 33, 21, 36, 16};
     int lotto_plus2[7] = {40, 17, 14, 21, 35, 32, 30};
    
    
    
    
     int mynumbers[6], i, j, num_matches = 0;
     int bonus = 0;                // bool bonus = false;
    
    
     // possible range of entry is 1 to 45
     for(i = 0; i < 6; i++)
     {
       do
       {
       printf("Enter your lotto number %d\n", i + 1);
       scanf("%d", &mynumbers[i]);
       }
       while(mynumbers[i] <= 0 || mynumbers[i] >= 46);
     }
    
    
    // check lotto results here.
    // bonus number will be at position 6
    // check mynumbers against bonus number in each draw
      for(i = 0; i < 6; i++)
      { 
         if(mynumbers[i] == lotto[6])  // lotto[6] has bonus number
             bonus = 1;         // bonus = true;
      } 
    
    
    
    
    return 0;
    }
    sorry here it is its not much :/

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That's a pretty good start.

    You might want to consider using nested loops to achieve the results.

    You need to develop an algorithm. Work it out on paper to figure out the logic before coding. One way to approach it might be like so:

    Code:
    declare a match counter (initialize to zero)
    
    compare the first user number to the first set of winning numbers
    if a match is found, increment the match counter
    
    compare the second user number to the first set of winning numbers
    if a match is found, increment the match counter
    
    etc
    This is by no means comprehensive, but merely to illustrate how you should think about the problem in order to solve it.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    thanks very much il try that

    im sorry but i don't understand what you mean by algorithm?

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    You can use two for's to make the comparison and counters to count how many times you scored.

    Code:
     
       
      #ifndef   SIZEMYLOTTO 
        #define  SIZEMYLOTTO 6 
      #endif 
    
     #ifndef SIZELOTTO7
       #define SIZELOTTO7  7 
     #endif 
    
      int lotto7Comparison(int myLotto[], int lotto7[]) 
      {
         int i, j, scored;
         scored = 0;
         for(i = 0; i < SIZEMYLOTTO; i++) 
          for(j = 0; j < SIZELOTTO; j++) 
          { 
              if(myLotto[i] == lotto7[j]) 
               scored++;
          } 
          return scored;  
      }
    
    int main(void) 
    { 
       int myLotto[SIZEMYLOTTO];
       int lotto7[SIZELOTTO7];
       .
       . 
       . 
       if(lotto7Comparison(myLotto, lotto7) == 7 ) 
         printf("jackpot!");
       if(lotto7Comparison(myLotto, lotto7) == 6)
         . 
         .
         . 
      return 0; 
    }
    the two for's ensure that every single number of your lotto is going to be compared with all the numbers of lotto7.

    also, please take the habit to write

    Code:
     
    int main(void) 
    { 
    
      return 0; 
    }
    instead of

    Code:
     
    int main() 
    { 
    
      return 0; 
    }
    when you don't pass any arguments, you're saying to the function it accepts any amount of arguments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with the Programme (ARRAYS)
    By haiderali in forum C Programming
    Replies: 3
    Last Post: 01-28-2012, 01:46 PM
  2. plz help me making this programme
    By zunaira in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2009, 11:43 PM
  3. making a programme?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-13-2005, 06:40 PM
  4. making a programme
    By wart101 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2005, 04:52 AM
  5. Combinations, lotto, 6/49
    By Robert in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2002, 07:27 PM