Lotto problem

This is a discussion on Lotto problem within the C Programming forums, part of the General Programming Boards category; Long back there was one question related to Lotto http://cboard.cprogramming.com/showt...threadid=35508 Here is my code for that, can anybody optmize it ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Lotto problem

    Long back there was one question related to Lotto

    3$ paid for simple code

    Here is my code for that, can anybody optmize it further,

    Code:
    main()
    {
    	int i,k,j = 6;
    	int a[6];
    
    	while ( getch() == 'C')
    	{
    		j = 6;
    		while (j)
    		{
    			k = 6-j;
    			i = (rand())%47;
    			a[k] = i;
    
    			while ( k-- )
    			{
    				if ( a[k] == i )
    				{
    					k = 6-j;
    					i = (rand())% 47;
    					a[k] = i;
    				}
    			}
    			printf(" i = %d\t",i);
    			j--;
    		}
    		printf("\n");
    	}
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, I'm bored, so here's my attempt:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define GONE -1
    #define ASIZE(a) (sizeof((a)) / sizeof((a)[0]))
    #define YUMYUM while (getchar() != '\n')
    
    int main(void)
    {
      int numbers[50];
      int i, k;
      
      srand(time(NULL));
        
      for (i = 0; i < ASIZE(numbers); i++)
        numbers[i] = i + 1;
    
      printf ("Generate Numbers (y/n): ");
      while (getchar() == 'y')
      {
        YUMYUM;
        for (i = 6; i ; i--)
        {
          for (k = GONE; k == GONE; k = numbers[rand() % ASIZE(numbers)]);
          printf ("%d ", k);  
          numbers[k-1] = GONE;
        }
        printf ("\nGenerate Numbers (y/n): ");
      }
      
      return 0;
    }
    But it can only generate eight batches of six numbers before it goes a tad wrong
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    At 9th iteration, code display only two numbers and stops.

    Let me check why is this happening..

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Let me check why is this happening..
    You've got 5 minutes to find the bug.... come on... clock's ticking... (In case you didn't realise, I know what it is... it's a design fault
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Array size

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Don't you think that as a limitation?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Array size
    The size of the array does affect it, but it doesn't matter how big the array is, it will always stop at some point, because ........

    >>Don't you think that as a limitation?
    Yeah, I suppose so, either term is fine. It just depends if you're the user or the programmer. A user would say it's broken, and a programmer would say its a limitation/design issue.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check this $$$$ out:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    signed short int numbers[47] =
    {
    	 0, 1, 2, 3, 4, 5, 6, 7,
    	 8, 9,10,11,12,13,14,15,
    	16,17,18,19,20,21,22,23,
    	24,25,26,27,28,29,30,31,
    	32,33,34,35,36,37,38,39,
    	40,41,42,43,44,45,46
    };
    
    signed short int getnumber( void )
    {
    	static signed short int high = 47;
    	signed short int current = rand( ) % high--;
    	signed short int temp = numbers[current];
    	numbers[current] = numbers[high];
    	numbers[high] = -1;
    
    #ifdef DEBUG
    	{
    		/* If you're confused as to what's happening, enable debug.*/
    
    		int x;
    		for( x = 0; x < 47; x++ )
    			printf("%d%c", numbers[x], x%10==9?'\n':' ' );
    		getchar( );
    	}
    #endif
    	
    	return temp;
    }
    
    int main( void )
    {
    	signed short int winningnumbers[6] = { -1,-1,-1,-1,-1,-1 };
    
    	srand( time( 0 ) );
    
    	winningnumbers[0] = getnumber( );
    	winningnumbers[1] = getnumber( );
    	winningnumbers[2] = getnumber( );
    	winningnumbers[3] = getnumber( );
    	winningnumbers[4] = getnumber( );
    	winningnumbers[5] = getnumber( );
    
    	printf("Tonight's winning numbers are...\n");
    	printf("%d %d %d %d %d %d\n",
    		winningnumbers[0], winningnumbers[1],
    		winningnumbers[2], winningnumbers[3],
    		winningnumbers[4], winningnumbers[5] );
    
    	return 0;
    }
    *Cackles like a demon!*

    Please don't use this for homework.
    [edit]
    The only way to (probably) optimize this further would be to bypass the function call and repeat that code inline.
    [/edit]

    Quzah.
    Last edited by quzah; 03-13-2003 at 05:42 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What, no incredibly confusing #define's Quzah? Neat solution though...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Since the original does not call srand, and for me it generates the sequence {25,39,35,33,19,34}, how about this?
    Code:
    #include <stdio.h>
    int main(void)
    {
       int lotto[] = {25,39,35,33,19,34,0}, *number = lotto;
       while(*number)
          printf("%2d ", *number++);
       return 0;
    }
    But assuming we want different numbers to be picked, I might try this.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       int lotto[6],i,j;
       srand(time(NULL));
       for(j = 5; j >= 0; --j)
       {
          REDO:
          lotto[j] = rand() % 46 + 1; /* 1-46 */
          for(i = 5; i > j; --i)
          {
             if(lotto[i] == lotto[j])
             {
                goto REDO;
             }
          }
          printf("%2d ", lotto[j]);
       }
       putchar('\n');
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    goto REDO;
    That's just horrible. The idea was to optimize, not obfuscate. Still, it's always to see interesting to see other implementations.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by quzah
    Code:
    goto REDO;
    That's just horrible.
    Thank you.

    For my next trick I will use void main(void) in a completely 100% standard implementation!
    [Of course, it would be in a freestanding environment.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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