RSS Forums RSS

Comparing and searching two arrays

Please support our Pascal and Delphi advertiser: Programming Forums
Reply
Posts: 8
Reputation: smorton123 is an unknown quantity at this point 
Solved Threads: 0
smorton123 smorton123 is offline Offline
Newbie Poster

Comparing and searching two arrays

  #1  
Dec 2nd, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 68
Reputation: jsosnowski is an unknown quantity at this point 
Solved Threads: 11
jsosnowski's Avatar
jsosnowski jsosnowski is offline Offline
Junior Poster in Training

Re: Comparing and searching two arrays

  #2  
Dec 2nd, 2008
Originally Posted by smorton123 View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 748 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:13 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC