-3

I need to programm the Sieve of Eratosthenes in Delphi 5 as a school homework project. This is what i currently have:

There is one problem:

At the end of the last for-"loop" it says"if zahl[i] then write(i:8);".

I suppose it should give out the content but what I need is the following:
It adding the number in the array, if the content of the array is yes, to an edit.

Can anyone help?

var
  Form1: TForm1;
  iValue, iCode: Integer;
  index, anzahlgaeste: Integer;
  gaeste: array of string;
  zahl:array[1..1000] of boolean;
  i,j,grenze:integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     if n.text = '' then
          begin
               error.text := 'Feld ist leer';
               n.SetFocus;
          end
     else begin
          val(n.text, iValue, iCode);
          if iCode = 0 then
             begin
                  if StrToInt(n.text) < 2 then
                     begin
                          error.text := 'Zahl ist kleiner als zwei';
                          n.SetFocus;
                     end
                  else
                      begin
                           error.text := 'Erfolgreich';
                           //SetLength(gaeste, StrToInt(n.text));
                           //result.Caption := IntToStr(High(gaeste));
                           //for index := 0 to StrToInt(n.text) do
                           //    begin
                           //         result.text := result.text +     'gaeste[index]';
                           //    end;
                           //https://mathematikalpha.de/primzahlsieb-des-    eratosthenes
                           //result.text := IntToStr(sizeof(zahl));
                           grenze:=1000;
                                 fillchar(zahl,sizeof(zahl),true);
                                 i:=2;   //erste Streichzahl
                                 repeat
                                       j:=i+i;
                                       repeat
                                             zahl[j]:=false;
                                             j:=j+i; //nächste zu     streichende Zahl
                                       until j>grenze;
                                       inc(i);
                                       while zahl[i]=false do inc(i);
                                 until i>sqrt(grenze);
                                 for i:=2 to grenze do
                                     //if zahl[i] then write(i:8);
                                     //BoolToStr(Value: Boolean): String;
                                     //result.text := BoolToStr(zahl[i]);
                      end;
             end
          else
              begin
                   error.text := 'Keine (natürliche) Zahl';
                   n.SetFocus;
              end
     end;

end;

end.
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Keyinator
  • 111
  • 2
  • 10
  • "if the content of the array is yes" - what should that mean? And what keeps from using the rest of your written condition in code? – Nico Haase May 27 '18 at 16:38
  • I guess you mean something like this `if zahl[i] then edit1.Text := inttostr(i);` ? But then you should better use a stringlist or a stringgrid, otherwise the edit.text is always overwritten in the loop. – gammatester May 27 '18 at 17:39
  • 1
    Do you understand your homework? You are trying to generate all prime numbers up to a certain value. You therefore want to see all such numbers, so as gammatester says, you need a control to display a list - your choice on that. Once you understand that, the rest should be obvious. – Dsm May 29 '18 at 11:15
  • It's somewhat strange that Delphi 5 is still taught at school... My attempt at the sieve is [here](https://github.com/stijnsanders/primes) – Stijn Sanders May 29 '18 at 13:56

1 Answers1

0

You say about your task:

It adding the number in the array, if the content of the array is yes, to an edit.

That is not very clear, but I interprete it as:

If an element in the array is True add the index of the element to a TEdit control.

Since you will have several numbers to list in the TEdit, formatting it as a CSV seems reasonable. Thus, you could do:

for i:=2 to grenze do
  if zahl[i] then Edit1.Text := Edit1.Text + IntToStr(i) + ',';
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54