I can’t believe that 1 week has gone by already. C is a little harder than I thought. I’ve read the first 8 chapters so I tried to write my first C program. It is a simple random number generator for the lottery. I am still having a couple problem generating numbers that don’t repeat in a list. Here is the code:
#include
#include //This is to bring in the declaration of clock()
#include //This is to bring in the declarations of srand() and rand()
int WPicks( void );
int DPicks( void );
void Weekly( void );
void Daily( void );
int NoSame[100];
int main (int argc, const char * argv[]) {
srand( clock() );
Weekly();
//printf("\n");
//Daily();
return 0;
}
void Weekly(){
int pick, i, j;
int k;
for (k=1; k<=1; k++) {
NoSame[k] = WPicks();
}
for (j=1; j<=5; j++){
for (i = 1; i <= 6; i++) {
pick = WPicks();
if ( pick != NoSame[i] )
NoSame[i] = pick;
else
i -= 1;
}
for (i=1; i<=6; i++) {
printf( "%d\t",NoSame[i]);
}
printf("\n");
for (k=1; k<=6; k++) {
NoSame[k] = 0;
}
}
}
void Daily(){
int pick, i, j;
for (j=1; j<=5; j++){
for (i=1; i<=3; i++) {
pick = DPicks();
printf( "%d\t",pick);
}
printf("\n");
}
}
int WPicks( void ) {
return (rand() % 56) + 1;
}
int DPicks( void ) {
return (rand() % 10);
}
I am having problems with the Weekly() function. If you see the problem leave a comment and let me know.