-
Lotto problem
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 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");
}
}
-
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 ;)
-
At 9th iteration, code display only two numbers and stops.
Let me check why is this happening..
-
>>Let me check why is this happening..
You've got 5 minutes to find the bug.... come on... clock's ticking... :D (In case you didn't realise, I know what it is... it's a design fault ;)
-
-
Don't you think that as a limitation?
-
>>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.
-
Check this $$$$ out: :D
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.
-
What, no incredibly confusing #define's Quzah? :D Neat solution though...
-
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;
}
-
:D That's just horrible. The idea was to optimize, not obfuscate. Still, it's always to see interesting to see other implementations.
Quzah.
-
Quote:
Originally posted by quzah
:D 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.]