I've been having more trouble with my lottery program. At the moment I've got the program to take in the users numbers (basically any numbers between 0-49 they want for the lottery draw). These are stored in the array 'user'. I also have the random numbers created by the program created in the array 'numbers'.

What I want the program to do is search the array 'numbers' for each of the numbers in the array 'user'. At the moment the program only finds a match if the numbers are in the exact same position in the other array. If the number 7 say was at position 3 in both arrays it would be recognised, however if 7 was at position 3 in one array and position 5 in the other the program would not recongise the number was in both arrays . Any help would be much appreicated.

I am using Pascal in Xcode

program test;
   uses
     EmptyPlaceHolderUnit;

type
a = array[1..7] of integer;
b = array[1..7] of integer;

   var
      yn : string;
      i : integer;
      k, j, tmp: integer;
      numbers: a;
      user: b;
      counter: integer;

var
 subscript: integer;

      procedure initialise (var counter: integer);
    begin
        counter := 0;
    end;


    procedure start_game (yn: string; i:integer; numbers: a);


begin

  writeln ('  ');
  writeln (' And tonights Lottery Balls are...');
  writeln 
  ('  '); 

    randomize;

        for i := 1 to 7 do
                write(random(50),'  ');                {Selects 6 random integers from the range 0 - 49}
                    writeln(' ');

                end;

       procedure add_numbers (var numbers: a; k ,j, tmp: integer);      

begin
    writeln ('  ');
   for subscript := 1 to 7 do
      begin
        writeln('Please enter the lottery numbers');      {The program asks for the user }
        readln(numbers[subscript]);                             {to enter the numbers back into the program}
        end;


begin
   numbers[1] := numbers[1];
   numbers[2] := numbers[2];
   numbers[3] := numbers[3];
   numbers[4] := numbers[4];
   numbers[5] := numbers[5];
   numbers[6] := numbers[6];
   numbers[7] := numbers[7];

   for k := 1 to 6 do
      for j := k + 1 to 7 do
         if numbers[k] > numbers[j] then              {Sorts numbers into ascending order}
         begin
            tmp := numbers[k];
            numbers[k] := numbers[j];
            numbers[j] := tmp;
         end;

  writeln('  ');
  writeln('And tonights big money balls in ascending order are');
  writeln('  ');

   for k := 1 to 7 do

      writeln(k,': ',numbers[k]);               {displays numbers}
      end;
      end;

      procedure get_details (var user: b);
        var
            subscript: integer;
    begin
      writeln ('  ');
        for subscript := 1 to 7 do
            begin
                writeln('Please type in your numbers ');
                readln(user[subscript]);
            end;
        end;


procedure search_for_numbers (numbers: a; user: b; var counter: integer);
        var
            subscript: integer;
    begin
        for subscript := 1 to 7 do
            begin
                if user[subscript] = numbers[subscript] then
                    counter := counter + 1;
            end;
    end;




procedure display_results (counter: integer);
    begin
        if counter = 0 then
            writeln('Unlucky! You have ', counter, ' numbers ')
        else
            writeln('Congratulations you have ', counter, ' number(s) ');
    end;



begin
    initialise(counter);
    start_game(yn, i, numbers);
    add_numbers(numbers, k ,j, tmp);
    get_details(user);
    search_for_numbers(numbers,user,counter);
    display_results(counter);
end.

Dani AI

Generated

Two things are happening in the thread: the match routine only compares identical subscripts, and the generated balls are only printed, not stored. is right that a nested comparison is needed; additionally, the routine that prints random numbers should assign them into the numbers array (and be declared with a var numbers: a parameter) so the drawn values actually exist for comparison.

A simple, safe Pascal routine that compares each user entry against all drawn numbers and avoids double-counting a user entry:

procedure search_for_numbers(const numbers: a; const user: b; var counter: integer);
var
  i, j: Integer;
  found: Boolean;
begin
  counter := 0;
  for i := 1 to 7 do
  begin
    found := False;
    j := 1;
    while (j <= 7) and (not found) do
    begin
      if user[i] = numbers[j] then
        found := True;
      j := j + 1;
    end;
    if found then
      inc(counter);
  end;
end;

Practical tips and alternatives:

  • Ensure the random draw is stored, e.g. declare procedure start_game(var numbers: a); and use numbers[i] := random(50); before displaying.
  • If duplicates in either array are possible and should be ignored, mark matched entries (a boolean used[] array) so the same drawn number is not matched multiple times.
  • For larger arrays, consider converting to a set of 0..49 and counting the intersection, or sort both arrays and use a two‑pointer merge to count matches (both faster than O(n^2) for bigger n).
  • Always validate inputs (range 0..49) and initialize counter := 0 before counting.

These changes address both the logic pointed out by and the storage/parameter issue present in 's code.

procedure search_for_numbers (numbers: a; user: b; var counter: integer);
var
subscript: integer;
begin
for subscript := 1 to 7 do
begin
if user[subscript] = numbers[subscript] then
counter := counter + 1;
end;
end;
end.

It only checks the matching subscript numbers because that is what you have told it to do. IF you want to compare each number in "user" with each number in "number" you must set up a second for loop within the first, similar to what you have above to sort numbers in the add_numbers method.

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.