It is necessary to find the saddle point of the matrix, which is filled by the user through the keyboard.
In the following code is the format of the matrix, in which rows and columns can be added or deleted according to the user's wishes. How can I go about entering the elements and finding the saddle point?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text:='3';
Edit2.Text:='3';
Button1Click(Sender);
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,N,M:integer;
begin
N:=StrToInt(Edit1.Text);
M:=StrToInt(Edit2.Text);
for i:=1 To N Do
StringGrid1.Cells[i,0]:=IntToStr(i);
for i:=1 To M Do
StringGrid1.Cells[0,i]:=IntToStr(i);
StringGrid1.ColCount:=N+1;
StringGrid1.RowCount:=M+1;
StringGrid1.Cells[0,0]:='Matrix';
end;
end.
Taking Alvaro Montoro's answer to the question Asserting if a position of the matrix is a saddle point in language as a reference
C
, and taking into account that the user would enter the values in a StringGrid, as you state in the question, we are going to do the solution in several steps.Definition (taken from the above question : A saddle point
I declare a data type to store the array of integers:
I declare a method to extract the data from
StringGrid
and load it into a variable of typeTIntArray2
. This minimizes string/integer conversions and throws an error if the user has not entered the array correctly.It is important to note that, in the solution, the first dimension of the matrix is the rows and the second is the columns.
I declare a method that has a sub-function that determines if a given position is a saddle point, based on the answer already referred to. The method goes through the matrix given as a parameter looking for saddle points and when finding them, it adds a line to a memo indicating its coordinates.
Finally, in a button's OnClick event handler, I make the necessary calls to perform the task:
This is what it looks like at runtime, entering the same values as the example given in the question.