PDA

Просмотр полной версии : Вывод двумерного массива в Memo



ЮрийIII
23.11.2011, 09:01
Добрый день. Начал писать программу для учебы. Задача заключается в том, что необходимо создать матрицу i на j, i и j (размерность) вводятся пользователем. А сами элементы матрицы i0j0, i1j0 и т.д. должны заполняться рандомно. Вопрос: Как организовать такой массив и как это вывести в Memo?
Вот код, в который необходимо записать.
Может я неправильно мемо взял, может есть другие функции для этого в делфи?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
BitBtn1: TBitBtn;
Memo1: TMemo;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
type matr=array[0..100,0..100]of integer;
var mt:matr;
x:byte;
implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);

var
i : integer;
TxtFile : TextFile;
begin
Memo1.Lines.Add('Юрий');
AssignFile(TxtFile, 'textfile.txt');
Rewrite(TxtFile);
for i:=1 to Memo1.Lines.Count do
begin
if (i mod 2=0) then
Writeln(TxtFile, Memo1.Lines[i-1]);
end;
closefile(txtfile);

end;

end.

Хыиуду
23.11.2011, 10:14
А зачем именно Memo, для этого гораздо удобнее использовать StringGrid.
Массив заполняется банально
for i:=1 to imax
for j:=1 to jmax
mt[i, j]:=random(100);
А вывод - ну, например, в цикле в i-й строке вывести все числа mt[i, 1] .. mt[i, jmax]

ЮрийIII
26.11.2011, 13:19
Не получается вывести матрицу, теперь уже в StringGrid. Подскажите пожалуйста как. Вот мой чахлый код:

var
Form1: TForm1;
type matr=array[0..100,0..100]of integer;
var mt:matr;
x:byte;
implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);

var
{z : integer;}
i,j,imax,jmax: integer;
mt: array [1..254,100..10000] of integer;
{TxtFile : TextFile;}
begin
imax:=100;
jmax:=10000;
i:=StrToInt(Edit1.Text);
j:=StrToInt(Edit2.text);
for i:=1 to imax do
begin
for j:=100 to jmax do
begin
mt[i,j]:=random(100);
begin
{StringGrid1.Cells[mt[i,1]]; }
end;
end;
{StringGrid1('Þðèé');
AssignFile(TxtFile, 'textfile.txt');
Rewrite(TxtFile);
for z:=1 to Memo1.Lines.Count do
begin
if (z mod 2=0) then
Writeln(TxtFile, Memo1.Lines[z-1]);
end;
closefile(txtfile);

end;}
end;
end;
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
StringGrid1.RowCount:=100;
StringGrid1.ColCount:=9900;
end;

end.

Хыиуду
06.12.2011, 13:09
А зачем вам j от 100 до 10000? Почему бы не начать с единицы или нуля?
Вывод - что-то вроде StringGrid1.Cells[i,j].value:=mt[i,j]