My question is:

to create an array of 6 lotto numbers. The numbers should be between 1 and 40 and you are not allowed to put into the array any number that is already in it.
Hint: You are using an array of 6 numbers so that’s a loop
generate a temporary number
while that number is already in the lotto array regenerate it
put your temporary number in the current slot of the array

{
Question
lotto game,one number only once,6 numbers,between 1..40!

}
Program Solution01;

Var Temp,i,j:Integer;
    Lotto:Array[1..6]Of Integer;
    H:Set of Byte;

Begin
   Randomize;
   {try to fill with random numbers}
   H:=[];   {empty}
   For i:= 1 To 6 Do Begin
      Lotto[i]:=1+Random(40);{1..40}
      If (Lotto[i] In H) Then Begin
         i:=i-1;
      End
      Else Begin
         H:=H+[Lotto[i]];
      End;
   End;
   {direct arranging}
   For i:=1 To 5 Do Begin
      For j:=i+1 To 6 Do Begin
        If (Lotto[i]>Lotto[j]) Then Begin
           Temp:=Lotto[i];
           Lotto[i]:=Lotto[j];
           Lotto[j]:=Temp;
        End;
      End;
   End;
  {writes the arranged results...}
   For i:=1 To 6 Do WriteLn(i:2,'. ',Lotto[i]:2);
   ReadLn;
End.

{
-= Note By FlamingClaw =-

All programs created by me  are written and tested
in Dev Pascal and/or Turbo Pascal 7.0
Of course working!
-= Created By FlamingClaw =-
-=2009.04.01=-
}
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#define MAX_NUM 40
#define NUM_LOTTO_BALLS 6
#define NUM_POWER_BALLS 8

void Rsleep( float period )
{
        int seconds, useconds;
        double TimeNow;
        struct timeval tvCurrent;

        seconds = itrunc(period);
        useconds = (period - seconds) * 1000000;

        gettimeofday(&tvCurrent, NULL);
        TimeNow = tvCurrent.tv_sec + (tvCurrent.tv_usec)/1000000.0;

        sleep( seconds );
        usleep( useconds );

        gettimeofday(&tvCurrent, NULL);
        TimeNow = tvCurrent.tv_sec + (tvCurrent.tv_usec)/1000000.0;

}

void main(int argc, char* argv[]) {


        struct tm tm;
        time_t t;

        int n;
        int numlines;
        int iBall1, iBall2, iBallTmp;
        int index_of_min;
        int iNumBalls;
        int i,j,draw,x,y;
        int isDrawn;
        int l_draw[NUM_LOTTO_BALLS] = {
                0,0,0,0,0,0     };


        if(argc!=2)
        {
                printf("Proper input is 'myLGen numlines'");
                exit(0);
        }
        else
                numlines=atoi(argv[1]);



        for (n=1; n<=numlines; n++) {
                iNumBalls=0;
                for (i=0; i<NUM_LOTTO_BALLS; i++)
                        l_draw[i]=0;


                /*populate array with unique random numbers*/
                for (i=0; iNumBalls<NUM_LOTTO_BALLS; )
                {
                        draw=0;
                        Rsleep(3.0);
                        srand((unsigned)time(NULL));
                        srand((unsigned)time(NULL));
                        do {
                                draw=rand() % 41;
                        } while (draw==0);

                        printf("\ndraw=%d", draw);

                        isDrawn=0;
                        for (j=0; j<NUM_LOTTO_BALLS; j++)
                                if (draw==l_draw[j]) isDrawn=1;

                        if (!isDrawn) {
                                l_draw[i]=draw;
                                iNumBalls+=1;
                                i++;
                        }
                        srand(draw);
                }

                /*sort array*/
                for(x=0; x<NUM_LOTTO_BALLS; x++)
                {
                        index_of_min = x;
                        for(y=x; y<NUM_LOTTO_BALLS; y++)
                        {
                                iBall1 = l_draw[index_of_min];
                                iBall2 = l_draw[y];

                                /*Compare the 2 balls*/
                                if ( iBall1 > iBall2 ) index_of_min = y;
                        }/*end for*/

                        /*Do a swap to sort the 2 balls*/
                        iBallTmp = l_draw[x];
                        l_draw[x] = l_draw[index_of_min];
                        l_draw[index_of_min] = iBallTmp;
                }/*end sort*/


                /*print out the contents of the array*/
                printf("\nline %d = [ ", n);
                for (i=0; i<NUM_LOTTO_BALLS; i++)
                        printf("%2d ", l_draw[i]);
                printf("]\n");
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.