Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, Consider using StrToIntDef (String To Integer with a Default value) instead of StrToInt (String to Integer) this will yield the following...</p> <pre><code>value := StrToIntDef('Abcdef', 0); // value will be zero </code></pre> <p>vs</p> <pre><code>value := StrToInt('Abcdef'); // exception </code></pre> <p>But the question is do you want integers or floating point values for your temperatures? (eg 1 or 1.6?) If you want floating point values, maybe use StrToFloatDef...</p> <p>Second, I've seen lots of grads that use Delphi make this mistake, try to always use begin and end, it'll help... because it makes it really clear what you are doing inside a if/for/while and what you intend to do outside..</p> <pre><code>for i := 0 to lstTemp.Items.Count - 1 do begin // Sum all the items in the list Sum := Sum + StrToIntDef(lstTemp.Items[i], 0); end; </code></pre> <p>Next up your array is a bit pointless, the SetLength and adding items bit is OK, but its not very functional, when you could just use the items in the list. All you need to do is hang onto the max and min values.</p> <p>Then your last problem is that Average isn't going to be a whole integer, its going to have a fractional part. Eg. 5 divided by 2 is 2.5, not 2 and not 3. You could use trunc to return just the integer part, or change Average so that its a floating point number...</p> <pre><code>for K:=0 to lstTemp.Items.Count-1 do begin if (StrToIntDef(lstTemp.Items[K], 0) &gt; Max) then begin Max := StrToIntDef(lstTemp.Items[K], 0); end; if (StrToIntDef(lstTemp.Items[K], 1000) &lt; Min) then // note, really high number begin Min := StrToIntDef(lstTemp.Items[K], 1000); end; end; {Calculate Average} Average := Trunc(Sum / Count); // do you really want to trunc this? I suspect not. if Min = 1000 then // just incase begin Min := 0; end; </code></pre> <p>The final problem you will face is that your always setting the text of the same text box...</p> <pre><code>edtAvg.Text:=IntToStr(Average); //Display Average edtMin.Text:=IntToStr(Min); //Display Minimum Temp. (I assume this is supposed to be edtMin) edtMax.Text:=IntToStr(Max); //Display Maximum Temp. (I assume this is supposed to be edtMax) </code></pre> <p>I suppose the final improvement I'd make is noticing that you only need one for loop...</p> <pre><code>for K:=0 to lstTemp.Items.Count-1 do begin // Sum all the items in the list Sum := Sum + StrToIntDef(lstTemp.Items[K], 0); if (StrToIntDef(lstTemp.Items[K], Low(Integer)) &gt; Max) then // A really low value begin Max := StrToIntDef(lstTemp.Items[K], Low(Integer)); end; if (StrToIntDef(lstTemp.Items[K], High(Integer)) &lt; Min) then // A really high value begin Min := StrToIntDef(lstTemp.Items[K], High(Integer)); end; end; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload