944,006 Members | Top Members by Rank

Ad:
Dec 2nd, 2008
0

Comparing and searching two arrays

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008
Dec 2nd, 2008
0

Re: Comparing and searching two arrays

Click to Expand / Collapse  Quote originally posted by smorton123 ...
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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
jsosnowski is offline Offline
68 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Exsist table in mdb file
Next Thread in Pascal and Delphi Forum Timeline: acces violation at address 004b84 in module "SecurityScreensaver.exe"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC