
Zekopavsa
7 год назад
2. Дана действительная квадратная матрица порядка n, найти наименьший и наибольший элементы матрицы, и поменять их местами друг с другом. На делфи
ОТВЕТЫ

Malygin
Jul 2, 2019
Unit Main;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);var i, j, n: integer; min, max, var_bl: real; a: array of array of real;beginMemo1.Clear;if TryStrToInt(Edit1.Text,n) thenbegin n:=StrToInt(Edit1.Text); if nlt;1 then begin Application.MessageBox(Ошибка!,,0); Exit; end; SetLength(a,n); for i:= 0 to n-1 do SetLength(a[i],n); Randomize; for i:= 0 to n-1 do for j:=0 to n-1 do a[i,j]:=Random(100)-30; min:= a[0,0]; max:= a[0,0]; for i:= 0 to n-1 do begin for j:=0 to n-1 do begin //Memo1.Text:=Memo1.Text+FloatToStrF(a[i,j],ffFixed,5,3)+ ;//Форматированный вывод Memo1.Text:=Memo1.Text+FloatToStr(a[i,j])+ ; //обычный вывод end; Memo1.Lines.Add(); end;
for i:= 0 to n-1 do for j:=0 to n-1 do begin if(a[i,j]lt;min) then min:=a[i,j]; if(a[i,j]gt;max) then max:=a[i,j]; end; Memo1.Lines.Add(Min=+FloatToStr(min)+, Max=+FloatToStr(max)); for i:= 0 to n-1 do for j:=0 to n-1 do begin if(a[i,j]=min) then begin a[i,j]:= max; Continue; end; if(a[i,j]=max) then a[i,j]:=min; end; Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add(Были поменяны наименьший и наибольший элемент массива); Memo1.Lines.Add(); for i:= 0 to n-1 do begin for j:=0 to n-1 do begin //Memo1.Text:=Memo1.Text+FloatToStrF(a[i,j],ffFixed,5,3)+ ;//Форматированный вывод Memo1.Text:=Memo1.Text+FloatToStr(a[i,j])+ ; //обычный вывод end; Memo1.Lines.Add(); end; Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add();end else Application.MessageBox(Ошибка!,,0);end;
end.
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);var i, j, n: integer; min, max, var_bl: real; a: array of array of real;beginMemo1.Clear;if TryStrToInt(Edit1.Text,n) thenbegin n:=StrToInt(Edit1.Text); if nlt;1 then begin Application.MessageBox(Ошибка!,,0); Exit; end; SetLength(a,n); for i:= 0 to n-1 do SetLength(a[i],n); Randomize; for i:= 0 to n-1 do for j:=0 to n-1 do a[i,j]:=Random(100)-30; min:= a[0,0]; max:= a[0,0]; for i:= 0 to n-1 do begin for j:=0 to n-1 do begin //Memo1.Text:=Memo1.Text+FloatToStrF(a[i,j],ffFixed,5,3)+ ;//Форматированный вывод Memo1.Text:=Memo1.Text+FloatToStr(a[i,j])+ ; //обычный вывод end; Memo1.Lines.Add(); end;
for i:= 0 to n-1 do for j:=0 to n-1 do begin if(a[i,j]lt;min) then min:=a[i,j]; if(a[i,j]gt;max) then max:=a[i,j]; end; Memo1.Lines.Add(Min=+FloatToStr(min)+, Max=+FloatToStr(max)); for i:= 0 to n-1 do for j:=0 to n-1 do begin if(a[i,j]=min) then begin a[i,j]:= max; Continue; end; if(a[i,j]=max) then a[i,j]:=min; end; Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add(Были поменяны наименьший и наибольший элемент массива); Memo1.Lines.Add(); for i:= 0 to n-1 do begin for j:=0 to n-1 do begin //Memo1.Text:=Memo1.Text+FloatToStrF(a[i,j],ffFixed,5,3)+ ;//Форматированный вывод Memo1.Text:=Memo1.Text+FloatToStr(a[i,j])+ ; //обычный вывод end; Memo1.Lines.Add(); end; Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add(); Memo1.Lines.Add();end else Application.MessageBox(Ошибка!,,0);end;
end.
90
Смежные вопросы: