Arsip: nilai minimum, maximum array dan matrix

 
user image
more 16 years ago

yadi

bro,,,,,n rekan2,,, gimana ya buat cari nilai minimum dan maximum array dinamis dan matrix dinamis,, misal

 baris : array  of real = (0,1,2,3,4,5,6,7,8,9,10);
          min = 0;
          max= 10;
 matrix : array of real =
             ((0,1,2,3),  
              (255,3,4,5),
              (50,8,2,1),
              (4,3,2,1));
          min = 0;
          max = 255;
thank's ya ...wat semuanya... :D
user image
more 16 years ago

pebbie


type
   TRealArray = array of real;
   TRealMatrix = array of array of real;
function min2(a,b:real):real;begin
  if a<b then result := a else result := b;
end;
function max2(a,b:real):real;begin
  if a>b then result := a else result := b;
end;
var
  j, i : integer;
  arrmin, arrmax : real; 
  matmin, matmaks : real;
  baris : TRealArray;
  matrix : TRealMatrix;
begin
  arrmin := MAXINT;
  arrmax := -arrmin;
  for j := low(baris) to high(baris) do begin
     arrmin := min2(arrmin, baris[j]);
     arrmax := max2(arrmax, baris[j]);
  end;
 
   matmin := MAXINT;
  matmaks := -matmin;
  for j := low(matrix) to high(matrix) do begin
     for i := low(matrix[j]) to high(matrix[j]) do begin
         matmin := min2(matrix[j][i], matmin);
         matmaks := max2(matrix[j][i], matmaks);
     end;
  end;
end;
user image
more 16 years ago

yadi

Thank's mas @pebbie,,, ta cobain dulu ya,,,,, tanya lagi ya kalo ada kesulitan... :D
user image
more 16 years ago

yadi

thank's ya,,,sempet ga tau juga he..he,,, cara gunakan fungsinya... tapi dah bisa n mudah2an tepat exactly... ta modif kalo ga tepat.rekan2 semuanya kasih tau..ya...

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  PArray = Array  of real;
  PMatrik = array  of real;
  TRealArray = array of real;
  TRealMatrix = array of array of real;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Button2: TButton;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  barisarray : PArray =
  (-1,2,3,4,5,6,7,8,9,10,11);
  matriksku : PMatrik =
  ((1,2,3,4),
   (255,3,4,1),
   (3,2,1,4),
   (4,3,2,0));
implementation
{$R .dfm}
function Rata2baris(x : PArray):real;
var
 i,jumlah,panjang : integer;
 hasil : real;
begin
  jumlah := 0 ;
  
  for i := 0 to length(x) - 1 do
  begin
       jumlah := jumlah + round(x[i]);
  End;
  panjang := length(x);
  hasil := jumlah/panjang;
  Rata2baris := hasil;
end;
function Rata2matrix(x1: PMatrik): real ;
var
 i,j,jumlah,nilai : integer;
 hasil : real;
begin
  jumlah := 0 ;
  for i := 0 to length(x1) - 1 do
  begin
   for j:= 0 to length(x1[0])-1  do
   begin
       jumlah := jumlah + round(x1[i,j]);
   End;
  End;
  nilai := length(x1) length(x1[0]);
  hasil := jumlah/nilai;
  Rata2matrix := hasil;
End;
function min2(a,b:real):real;
begin
if a<b then
   result := a
else
   result := b;
end;
function max2(a,b:real):real;
begin  if a>b then
   result := a
else
   result := b;
end;
function nilaimax( matrix : PMatrik):Real;
var
  j, i : integer;
  matmin, matmaks : real;
begin
    matmin := MAXINT;
    matmaks := -matmin;
    for j := low(matrix) to high(matrix) do
    begin
      for i := low(matrix[j]) to high(matrix[j]) do
      begin
         matmaks := max2(matrix[j][i], matmaks);
      end;
         result := matmaks;
    end;
end;
function nilaimin( matrix : PMatrik):Real;
var
  j, i : integer;
  matmin : real;
begin
   matmin := MAXINT;
  // matmaks := -matmin;
    for j := low(matrix) to high(matrix) do
    begin
      for i := low(matrix[j]) to high(matrix[j]) do
      begin
         matmin := min2(matrix[j][i], matmin);
      end;
         result := matmin;
    end;
end;
function nilaiminbaris( baris : PArray):Real;
var
  j : integer;
  arrmin : real;
begin
  arrmin := MAXINT;
 // arrmax := -arrmin;
  for j := low(baris) to high(baris) do
  begin
    arrmin := min2(arrmin, baris[j]);
  end;
    result := arrmin;
end;
function nilaimaxbaris( baris : PArray):Real;
var
  j : integer;
  arrmin, arrmax : real;
begin
  arrmin := MAXINT;
  arrmax := -arrmin;
  for j := low(baris) to high(baris) do
  begin
    arrmax := max2(arrmax, baris[j]);
  end;
    result := arrmax;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  nilairata2 : real;
  nilairata2gambar : real;
begin
  nilairata2 := Rata2baris(barisarray);
  edit1.Text := FloatToStr(nilairata2);
  nilairata2gambar :=  Rata2matrix(matriksku);
  edit2.Text := Floattostr(nilairata2gambar);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Minbaris, MaxBaris : Real;
  MinMatrix, MaxMatrix : Real;
begin
  MaxBaris := nilaimaxbaris(barisarray);
  edit3.Text := FloatToStr(MaxBaris);
  Minbaris := nilaiminbaris(barisarray);
  edit4.Text := FloatTostr(MinBaris);
  MaxMatrix := NilaiMax(matriksku);
  edit5.Text := FloatToStr(MaxMatrix);
  MinMatrix := NilaiMin(matriksku);
  Edit6.Text := FloatToStr(MinMatrix);
end;
end;
 :D   
more ...
  • Pages:
  • 1
Share to
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com