?
Solved

How to find the bigger number.

Posted on 2006-07-04
5
Medium Priority
?
208 Views
Last Modified: 2010-04-05
Hi...

This is easy i guess... I'm just not in thinging mode... right now and i want it fast.

So...
I have 120 integers and i want to find the 3 bigger numbers.
I need a smart way to do this with few lines.

Thanks in advance.
0
Comment
Question by:CodedK
[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
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 17038552
to find the bigger of 2 numbers you can use
uses math;
i := max(j, k);

1 way for you to get the top 3 would be to have the numbers in a sorted list
eg.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, math,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    numberlist: TList;
  end;

var
  Form1: TForm1;

function comparenumbers(Item1, Item2: Pointer): Integer;

implementation

{$R *.DFM}

function comparenumbers(Item1, Item2: Pointer): Integer;
    begin
        if Integer(Item1) < Integer(Item2) then result := -1
        else if Integer(Item1) > Integer(Item2) then result := 1
        else result := 0;
    end;

procedure TForm1.FormCreate(Sender: TObject);
    begin
        numberlist := TList.create;
// add some numbers
        numberlist.add(pointer(10));
        numberlist.add(pointer(8));
        numberlist.add(pointer(2));
        numberlist.add(pointer(7));
        numberlist.add(pointer(9));
        numberlist.add(pointer(1));
        numberlist.add(pointer(3));
    end;

procedure TForm1.FormDestroy(Sender: TObject);
    begin
        numberlist.Free;
    end;

procedure TForm1.Button1Click(Sender: TObject);
    var
        i: integer;
    begin
// make sure the numbers are sorted
        numberlist.Sort(comparenumbers);
// now grab the top 3 numbers and display them
        for i := (numberlist.count -1) downto (numberlist.count -3) do
          memo1.lines.add(inttostr( integer(numberlist[i])));
    end;

end.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17038610
I know you said "a few lines" but really it's just "sort" then the for loop :-)
here's a way to do it assuming the get the numbers from somewhere else

procedure TForm1.Button2Click(Sender: TObject);
    var
        sl: TStringList;
        i, value: integer;
        s: string;
    begin
        sl := TStringlist.create;
        try
            sl.sorted := True;
            i := 0; // first index from whatever source of numbers you have
            while (i < numberlist.count) do  // last index from whatever source of numbers you have
            begin
// get the number using index i from your source numbers and convert it to a 10 length padded string
                SL.Add(Format('%10d', [ integer(numberlist[i]) ] )); // if you need bigger numbers, use a number > %10
                inc(i); // go to the next number
            end;
// now grab the top 3 numbers and display them
            for i := (sl.count -1) downto (sl.count -3) do
            begin
                value := StrToInt( sl[i] );
                memo1.lines.add(inttostr( value ));
            end;
        finally
            sl.free;
        end;
    end;
0
 
LVL 17

Assisted Solution

by:mokule
mokule earned 500 total points
ID: 17039472
Not so briliant but possibly faster method.

procedure TForm1.Button1Click(Sender: TObject);
const
  numcount = 100;
var
  intnum: array[0..numcount-1] of integer;
  i: integer;
  b1,b2,b3: integer;
begin
// fill in the array
  for i := 0 to numcount-1 do
    intnum[i] := random(1500);

  if intnum[0] > intnum[1] then
    begin
    b1 := intnum[0];
    b2 := intnum[1];
    end
  else
    begin
    b1 := intnum[1];
    b2 := intnum[0];
    end;
  b3 := intnum[2]-1;

  for i := 3 to numcount-1 do
    begin
    if intnum[i] > b3 then
      begin
      if intnum[i] > b2 then
        begin
        b3 := b2;
        if intnum[i] > b1 then
          begin
          b2 := b1;
          b1 := intnum[i];
          end
        else
          b2 := intnum[i];
        end
      else
        b3 := intnum[i];
      end;
    end;
  Memo1.lines.Add(IntToStr(b1));   // biggest number
  Memo1.lines.Add(IntToStr(b2));   // second biggest
  Memo1.lines.Add(IntToStr(b3));   // third bigest
end;
0
 
LVL 16

Author Comment

by:CodedK
ID: 17039804
Thank you both.

There is a small problem... I need to know the var that holds the bigger value...
Example : bigger 3 =>(a14, a87, a2)... not only the value.
---------------------------------------------

Since all the numbers will be finally loaded in a Chart maybe there is another way...

X axis represents the variable (a0, a1, a2...a120).

I've loaded all the vars in a TChart !
So with the command :

Chart1.SeriesList.Series[0].MaxYValue; .... i will find the bigger number.

Is there a command for this :
(For this Y which is the X) ???
(And with a for loop decreasing by 1 from the bigger y which is the next bigger y) ?

I hope i make sense.... Thank you very much :)
0
 
LVL 16

Author Comment

by:CodedK
ID: 17039859
Forget it... Thank you for your time :)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

765 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