DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Pascal and Delphi (http://www.daniweb.com/forums/forum124.html)
-   -   Comparing and searching two arrays (http://www.daniweb.com/forums/thread160402.html)

smorton123 Dec 2nd, 2008 8:12 am
Comparing and searching two arrays
 
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.

jsosnowski Dec 2nd, 2008 12:23 pm
Re: Comparing and searching two arrays
 
Quote:

Originally Posted by smorton123 (Post 748737)
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.


All times are GMT -4. The time now is 4:18 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC