Recent

Author Topic: Problem with seting size of array using Edit  (Read 3571 times)

saintraa

  • Newbie
  • Posts: 3
Problem with seting size of array using Edit
« on: June 16, 2015, 01:28:34 pm »
Hello!

Could you please help me with following problem:

Goal : I need to set the dimension of a dynamic array via the component Edit, assign a value to each element of the array and do some operations . Print the specified array and the result is an array of operations on a file.

Error1: When I press button1 ( specify an array and write it to a file ) I get an error message when the dimension of the array is equal to the 5 . If the first set any other value , the error does not appear. In another case , if the first set any other value , then the value of 5 , the error does not appear.
http://postimg.org/image/w38cchlj7/
http://postimg.org/image/nv87y6pb1/
 
Error2: When in the menu, select the processing method of the array ( TForm1.MenuItem19Click ) , an error occurs
http://postimg.org/image/vv7gtmgoz/

Here is a code :
Code: [Select]
uses ShellApi, Math;
var OdnDin : array of integer; //Global array declaration
Code: [Select]
procedure TForm1.MenuItem8Click(Sender: TObject);//Задание ОднДинам
begin
  Label1.Visible := true;     //Tip component Edit
  Edit1.Visible := true;       //Enter the dimensions of the array
  Button1.Visible := true;  //Create an array , and write to the file
end;
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);//Кнопка задания ОднДинам
var
  n, i : byte;
begin
  AssignFile(f,'file.txt');
  Rewrite(f);
  n := StrToInt(Edit1.Text);
  SetLength(OdnDin,n);
  for i := 1 to n do
  begin
    OdnDin[i] := Random(20) - 10;
    Writeln(f,OdnDin[i]);
  end;
  OdnDin := NIL;
  CloseFile(f);
  Button2.Visible := true; //Button to opened files from the form to show
end;
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShellExecute(
  handle,
  'open',
  'c:\windows\notepad.exe',
  'file.txt',
  nil,
  1
);
  //ShellExecute(Handle, nil, 'c:\windows\notepad.exe','file.txt', nil, 0);
end;
Code: [Select]
procedure TForm1.MenuItem19Click(Sender: TObject);//Р ОднДинам п1
var i, j, kv, n : byte;
  sr : single;
begin
  kv := 1; sr := 0; j := 1; n := high(OdnDin);
  Append(f);
  for i := 1 to n do //Среднее знач квадратов отриц эл
      if OdnDin[i] < 0 then
      begin
        kv := sqr(OdnDin[i]);
        sr := (sr + kv)/j;
        j := j + 1;
      end
      else
        writeln(f,'В массиве нет эл < 0');
  writeln(f,'Среднее значение квадратов эл < 0 :');
  write(f,sr);
  CloseFile(f);
end;

Error3: Error http://postimg.org/image/uvw8pobu5/ while compiling when using the following code
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShellExecute(Handle, nil, 'c:\windows\notepad.exe','file.txt', nil, SW_SHOWNORMAL);
end;
It works only with following code
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShellExecute(
  handle,
  'open',
  'c:\windows\notepad.exe',
  'file.txt',
  nil,
  1
);
end;

With respect, saintraa.

saintraa

  • Newbie
  • Posts: 3
Re: Problem with seting size of array using Edit
« Reply #1 on: June 16, 2015, 01:52:53 pm »
Excuse me, just now I noticed

Code: [Select]
OdnDin := NIL;
Therefore , I will need to load an array from file do operations and append the result to a file or arrange it elsewhere.
« Last Edit: June 16, 2015, 02:51:29 pm by saintraa »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Problem with seting size of array using Edit
« Reply #2 on: June 17, 2015, 09:33:10 am »
A dynamic array always begins with 0
Code: [Select]
SetLength(OdnDin,n);
  for i := 1 to (n - 1) do
  begin
    OdnDin[i] := Random(20) - 10;
    Writeln(f,OdnDin[i]);
  end;

OdnDin := NIL should be SetLength(OdnDin,0)
« Last Edit: June 17, 2015, 01:18:59 pm by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Bart

  • Hero Member
  • *****
  • Posts: 5573
    • Bart en Mariska's Webstek
Re: Problem with seting size of array using Edit
« Reply #3 on: June 17, 2015, 12:07:31 pm »
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShellExecute(Handle, nil, 'c:\windows\notepad.exe','file.txt', nil, SW_SHOWNORMAL);
end;

It's not a good idea to hardcode the path to notepad, the folder C:\Windows might not even exist....
And since you use notepad to display the info (I guess), why not use a TMemo and load te contents of the file there?

Bart

osddeitf

  • New Member
  • *
  • Posts: 22
Re: Problem with seting size of array using Edit
« Reply #4 on: June 17, 2015, 01:09:26 pm »
SW_SHOWNORMAL not found because you didn't include unit windows in uses clause of your source 8)

saintraa

  • Newbie
  • Posts: 3
Re: Problem with seting size of array using Edit
« Reply #5 on: June 18, 2015, 10:03:28 am »
Thank you all for replies!

I solved Error2 and now all works fine! But I still don't understand why when I enter a value 5 in Edit component and then click on a Button1 appears an error.

Code: [Select]
procedure TForm1.MenuItem19Click(Sender: TObject);//Р ОднДинам п1
var i, j, kv, n, sum : integer;
  sr : single;
begin
  kv := 1; sr := 0; j := 0; sum := 0;
  AssignFile(f,'file.txt');
  Reset(f);
  n := FileSize('file.txt');
  SetLength(OdnDin,n);
  for i := 1 to n do //Среднее знач квадратов отриц эл
    begin
      Read(f,OdnDin[i]);
      if OdnDin[i] < 0 then
      begin
        kv := sqr(OdnDin[i]);
        sum := sum + kv;
        j := j + 1;
      end;
    end;
  if j <> 0 then
    sr := sum / j
  else
    MessageDlg('В массиве нет эл < 0', mtInformation,[mbOk],0);
  OdnDin := NIL;
  CloseFile(f);
  Append(f);
  Write(f,'Кол-во эл < 0 : '); Writeln(f,j);
  Write(f,'Среднее значение квадратов эл < 0 : '); Write(f,FloatToStrF(sr,ffFixed,3,2));
  CloseFile(f);
end;

 

TinyPortal © 2005-2018