Вывести на экран цифру, наиболее часто встречающуюся в данном массиве

Модераторы: Duncon, Naeel Maqsudov, Игорь Акопян, Хыиуду

Ответить
Andros_me
Сообщения: 1
Зарегистрирован: 20 дек 2010, 23:29

Условие задачи: Дан массив, состоящий из символов. Вывести на экран цифру, наиболее
часто встречающуюся в этом массиве.

вот мой код:

[syntax='Delphi']
unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Label1: TLabel;
Memo1: TMemo;
Label2: TLabel;
BitBtn1: TBitBtn;
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }
end;

var

Form1: TForm1;
a: array of char;
chastota: array of integer;
n,m,i,max:integer;


implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
begin
if edit1.text='' then exit;
m:=StrToInt(Edit1.Text);
StringGrid1.ColCount:=n+1;

for i:=1 to m do StringGrid1.Cells[0,i]:=' i='+IntToStr(i);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear
end;



procedure TForm1.BitBtn1Click(Sender: TObject);

begin
chastota:=0;
for i:=0 to n do
if a in ['0'..'9'] then
begin
inc(chastota);
end;
max:=0;
for i:=1 to 9 do if chastota>chastota[max] then max:=i;
Memo1.Text:=(IntToStr(max));

end;

end.
[/syntax]

при нажатие на кнопку выполнить выскакивает ошибка EAcessViolation... Подскажите пожалуйста как исправить чтобы заработало.. Буду очень признателен
Аватара пользователя
Игорь Акопян
Сообщения: 1440
Зарегистрирован: 13 окт 2004, 17:11
Откуда: СПБ
Контактная информация:

chastota: array of integer;
это динамический массив, нельзя просто присвоить в 0 и необходимо выделять память
читать справку по SetLength и dynamic arrays
Изображение
Ответить