?
Solved

Matching numbers in an array

Posted on 2003-02-27
4
Medium Priority
?
171 Views
Last Modified: 2010-04-04
what would be the most simple way to get the highest matching number in an array?
array1[1,2,3,4,6,8,12,14,21,24]
array2[1,2,3,4,5,6,7,8,10,12,15,16]
highest common number is 12.Can you make a SET from an array and us the in function? What is the most simple way to get the answer.
0
Comment
Question by:Lfteye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
ILE earned 90 total points
ID: 8038835
depends what u wont
if u wont speed that is a one question
and if u wont simle coding that is difrent question

the simple coding
array1 is a aray with   count1 members;
array2 is a aray with   count2 members;
m,mx is the same type as array1 and array2



var i,j:integer;

begin
m:=0;
for i:=1 to count1 do
begin
mx=m;
for j:=1 to count2 do
  begin
    if array1[i]=array2[j] then mx=array[i];
  end;
end;
if mx>m then m=mx
end;


0
 
LVL 6

Expert Comment

by:Hardi
ID: 8039738
If the arrays are sorted, may be something like this is faster to find the highest common number (search from the highest number backwards)

var i, j, count1, count2: integer;
    found: Boolean;

found := False;
i := count1;
while (not found) AND (i>0) do
begin
  j := count2;
  while (not found) AND (j>0) do
  begin
    if array1[i]=array2[j] then
    begin
      showMessage('Highest common number is '+
                  intToStr(array1[i]));
      found := True;
    end
    else if array2[j] < array1[i] then
      break;
    j := j-1;
  end;
  i := i-1;
end;

hope that helps
0
 
LVL 6

Expert Comment

by:Hardi
ID: 8039811
If the arrays are sorted, may be something like this is faster to find the highest common number (search from the highest number backwards)

var i, j, count1, count2: integer;
    found: Boolean;

found := False;
i := count1;
while (not found) AND (i>0) do
begin
  j := count2;
  while (not found) AND (j>0) do
  begin
    if array1[i]=array2[j] then
    begin
      showMessage('Highest common number is '+
                  intToStr(array1[i]));
      found := True;
    end
    else if array2[j] < array1[i] then
      break;
    j := j-1;
  end;
  i := i-1;
end;

hope that helps
0
 

Author Comment

by:Lfteye
ID: 8043516
This is the practice program I was fooling with.

program ReduceFraction(output);

var

   num1,num2,count,ctr1,ctr2,gcd,i,j : Integer;
   Key : Char;
   numerator :array[1..30] of integer;
   denominator :array[1..30] of integer;
begin
     Key:= 'y';
     while Key = 'y' do
     begin
     writeln('enter numerator :');
     readln(num1);
     writeln('enter denominator :');
     readln(num2);
     {--------------------------Numerator--------------------------------------}
     ctr1 := 0;
       for count := 1 to num1 do
            if num1 MOD count = 0 then
            begin
            ctr1 := ctr1 +1;
            numerator[ctr1] := count;    {---assign values to the array---}
            end;
      {-----------------------Denominator---------------------------------------}
                    ctr2 := 0;
                       for count := 1 to num2 do
                          if num2 MOD count = 0 then
                          begin
                          ctr2 := ctr2 +1;
                          denominator[ctr2] := count;  {---assign values to the array---}
                          end;
       {--------------------------------Find GCD-------------------------------------------}
                          for i := 1 to ctr1 do
                          begin
                                   for j := 1 to ctr2 do
                                   begin
                                   if numerator[i] = denominator[j] then
                                   gcd := denominator[j]
                                   end;
                          end ;
         {-------------------------Print reduced fraction---------------------------------------------}
                          writeln(num1,'/',num2,' reduced is ',num1 div gcd,'/',num2 div gcd,' the GCD is ',gcd);
                          writeln('continue (y)es/(n)o');
                          readln(Key);
         end;

end.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question