Recent

Author Topic: Need help on array plz  (Read 4755 times)

ilusion

  • New member
  • *
  • Posts: 7
Need help on array plz
« on: May 05, 2015, 08:48:46 am »
how calculate the number of  empty box in array   %)  :'( ???
« Last Edit: May 05, 2015, 09:03:18 am by ilusion »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Need help on array plz
« Reply #1 on: May 05, 2015, 09:23:37 am »
Here's a small example to show one way to do this.
In a new Lazarus project double-click the main form to generate an OnClick handler,and complete the unit as follows:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Forms, StdCtrls, strutils, Controls;

type

{ TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  r: integer = 0;
  s: string;
  arr: array of string;
  memo: TMemo;

  function CountEmpty: integer;
  var
    p: integer;
  begin
    Result:=0;
    for p:=Low(arr) to High(arr) do
      if (arr[p] = '') then
        Inc(Result);
  end;

begin
  Randomize;
  memo:=tmemo.Create(Self);
  memo.Clear;
  memo.Align:=alClient;
  memo.ScrollBars:=ssAutoVertical;
  memo.Parent:=Self;

  while (r = 0) do
    r:=Random(100);
  SetLength(arr, r);
  for r:=Low(arr) to High(arr) do begin
    s:=IntToRoman(r);
    if (Pos('X',s) > 0) then
      arr[r]:=s
    else arr[r]:='';
    if (arr[r] = '') then
      memo.Lines.Add('[empty]')
    else memo.lines.Add(arr[r]);
  end;
  memo.Lines.Insert(0, Format('%d empty positions in array (%d total positions)',
                              [CountEmpty, Length(arr)]));
end;

end.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Need help on array plz
« Reply #2 on: May 05, 2015, 10:06:08 am »
Code: [Select]
type
  TBox = record
    empty: boolean;
  end;

var
  boxes: array[0..9] of TBox;
  n, empties: integer;
begin
  empties:=0;
  for n:=low(boxes) to high(boxes) do
    if boxes[n].empty then inc(empties);
  writeln(empties);
end;

ilusion

  • New member
  • *
  • Posts: 7
Re: Need help on array plz
« Reply #3 on: May 05, 2015, 08:09:53 pm »
thank you but
He always display 0  %) !!!

Example
I have a table consists of 5 boxes i filling 2 boxes
I want to show me number of the boxes, which is still empty
( n = 3 )
« Last Edit: May 05, 2015, 09:39:47 pm by ilusion »

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Need help on array plz
« Reply #4 on: May 05, 2015, 09:12:09 pm »
you should show your code.

who is 'He'?

did you follow howardpc or User137? both? neither?
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

ilusion

  • New member
  • *
  • Posts: 7
Re: Need help on array plz
« Reply #5 on: May 05, 2015, 09:35:26 pm »
i follow howardpc

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Need help on array plz
« Reply #6 on: May 05, 2015, 09:43:09 pm »
i tried what howardpc posted...

after a couple of changes to his instructions, i got:

9 empty positions in array (41 total positions)
[empty]
[empty]
[empty]
[empty]
[empty]
[empty]
[empty]
[empty]
[empty]
IX
X
XI
XII
XIII
XIV
XV
XVI
XVII
XVIII
XIX
XX
XXI
XXII
XXIII
XXIV
XXV
XXVI
XXVII
XXVIII
XXIX
XXX
XXXI
XXXII
XXXIII
XXXIV
XXXV
XXXVI
XXXVII
XXXVIII
XXXIX
XL


did you get anything?
can you post your code?
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

ilusion

  • New member
  • *
  • Posts: 7
Re: Need help on array plz
« Reply #7 on: May 05, 2015, 10:00:54 pm »
I want the number of empty boxes only
Example

I have 5 boxes i filling 2 boxes
Write me  n = 3  (n = numbre of empty boxes )
Quote
program Untitled;
type
  prsonne = record
  name : string;
  empty: boolean;
  end;

var
  boxes: array[0..9] of prsonne ;
  n, empties: integer;
begin
      for n := 0 to 3 do
      begin
      writeln (' enter the first name : ' );
      readln(boxes[n].name);
      end;
  empties:=0;
  for n:=low(boxes) to high(boxes) do
    if boxes[n].empty then inc(empties);
  writeln(empties);
  readln;

end.

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Need help on array plz
« Reply #8 on: May 05, 2015, 10:15:20 pm »
you are not setting the boolean empty in the loop where you get the names, but are then checking empty in the second loop. of course, the results will be wrong.

i don't see a good reason to have the boolean part of the record - simply check for Name <> '' if you want to know how many empty records there are.
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

Eugene Loza

  • Hero Member
  • *****
  • Posts: 671
    • My games in Pascal
Re: Need help on array plz
« Reply #9 on: May 05, 2015, 10:17:09 pm »
Code: [Select]
boxes: array[0..9] of prsonne ;So you have 10 boxes total (not 5).
Code: [Select]
for n := 0 to 3 doThen you make the user input 4 first items (not 2)
He can also input an empty string, right?
so if the box is empty it should contain '' string:
Code: [Select]
empties:=0;
  for n:=low(boxes) to high(boxes) do
    if boxes[n].name='' then inc(empties);
  writeln(empties);
So, in this way you don't need empty: boolean; or you should "define" it at the input process (pay attention, the variable is not initialized, and default is 'false', this is why you get 0 empties).
« Last Edit: May 05, 2015, 10:18:55 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

ilusion

  • New member
  • *
  • Posts: 7
Re: Need help on array plz
« Reply #10 on: May 05, 2015, 10:42:41 pm »
What  i do now  help me  :'(
i want  know how many empty boxes there are

or

how many boxes records there are

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Need help on array plz
« Reply #11 on: May 05, 2015, 11:05:31 pm »
Code: [Select]
program Untitled;
{type
  prsonne = record
  name : string;
  empty: boolean;
  end;}

var
  boxes: array[0..9] of string;
  n, empties: integer;
begin
      for n := 0 to 3 do
      begin
      writeln (' enter name : ' );
      readln(boxes[n]);
      end;
  empties:=0;
  for n:=low(boxes) to high(boxes) do
    if boxes[n] = '' then inc(empties);
  writeln(empties);
  readln;

end.
         

you should re-consider why you are setting the input name loop to run 4 times... why 4? unless you did that only for test purpose.
« Last Edit: May 05, 2015, 11:08:49 pm by BitBangerUSA »
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Need help on array plz
« Reply #12 on: May 06, 2015, 10:05:40 am »
Code: [Select]
program Untitled;

type
  prsonne = record
  name : string;
  empty: boolean; //is useless
  end;

var
  boxes: array[0..9] of prsonne ;
  n, empties: integer;
begin
      //first read your array and set their properties to default
      for n := low(boxes) to high(boxes) do
        boxes[n].name := '';

      // the rest of your code
      for n := 0 to 3 do
      begin
      writeln (' enter the first name : ' );
      readln(boxes[n].name);
      end;
  empties:=0;
  for n:=low(boxes) to high(boxes) do
    if trim(boxes[n].name) = '' then inc(empties);
  writeln(empties);
  readln;

end.
functions low() and high() are also useless here, because you create a static array of 10 elements.
« Last Edit: May 06, 2015, 10:08:06 am by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Need help on array plz
« Reply #13 on: May 06, 2015, 10:23:52 am »
functions low() and high() are also useless here, because you create a static array of 10 elements.

No they are not. Whenever he decides to change the static dimensions of the array, this code will still work.

Bart

 

TinyPortal © 2005-2018