Comparing and searching two arrays

Reply

Join Date: Nov 2008
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

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
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

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC