# Sum, maximum and minimum of arrays

This example demonstrates the use of the list (array) functions sum, min and max. The integer versions of these Smart Pascal functions are used in this example, but you will note that the float versions are generated at the same time for use if necessary.

The rainfall figures given are average values for London.

The blocks

## XML Code of Blocks

```<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="procedures_defreturn" id="+8*MY2Qgyfr#ChKj%S[#" x="-263" y="-338">
<mutation>
<arg name="floatNum"></arg>
<arg name="intRange"></arg>
</mutation>
<field name="NAME">RoundTo</field>
<comment pinned="false" h="80" w="160">Rounds a number to intRange decimal places</comment>
<statement name="STACK">
<block type="variables_set" id="2![(zYAa3eU8A%RBFEjO">
<field name="VAR">floatPowerOfTen</field>
<value name="VALUE">
<block type="math_single" id="sa.ll@U8][(*UAbd`dLf">
<field name="OP">POW10</field>
<value name="NUM">
<field name="NUM">9</field>
<block type="variables_get" id="(mN=O)n-.:89~)9T_YpD">
<field name="VAR">intRange</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" id="FV_B%twk5HoO90UPD}sC">
<field name="VAR">floatRoundedNum</field>
<value name="VALUE">
<block type="math_arithmetic" id="gT^cKMj-agh:c/t~^Mhf">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block type="math_round" id="iHu|9a(oY^+5a|^5w5ZT">
<field name="OP">ROUND</field>
<value name="NUM">
<field name="NUM">3.1</field>
<block type="math_arithmetic" id=",*K_Jyw]]Ed#6t/CkL?%">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="2%gfi[z?WZg\$a0k;52,]">
<field name="VAR">floatNum</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="variables_get" id="PI2\$ToO}jebczg/MWneL">
<field name="VAR">floatPowerOfTen</field>
</block>
</value>
</block>
</value>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="variables_get" id="qPBVu8YaeVx#r+XqSQ%g">
<field name="VAR">floatPowerOfTen</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</statement>
<value name="RETURN">
<block type="variables_get" id="W3p@gNtBbOLRNcQQhOCK">
<field name="VAR">floatRoundedNum</field>
</block>
</value>
</block>
<block type="variables_set" id=")aKcp8vb.EX?Gq[dTL]+" x="-262" y="-188">
<field name="VAR">intRainfallArray</field>
<value name="VALUE">
<block type="lists_create_with" id="7j^Eyof!ijWgbt[bdiq+">
<mutation items="12"></mutation>
<block type="math_number" id="{M:z(m+njeaQeXZ1V5gs">
<field name="NUM">52</field>
</block>
</value>
<block type="math_number" id="#z./^Y\$HP(Rk?f4b1k6I">
<field name="NUM">39</field>
</block>
</value>
<block type="math_number" id="@xUXAhm7@:`eg]l-m:/f">
<field name="NUM">35</field>
</block>
</value>
<block type="math_number" id="btlG*Afh2D]~5JIPMvaZ">
<field name="NUM">43</field>
</block>
</value>
<block type="math_number" id="Eq^l@4%@56`u%yx-cp`2">
<field name="NUM">50</field>
</block>
</value>
<block type="math_number" id="Yc~h`i`)TF_.Cq!Jc^)I">
<field name="NUM">43</field>
</block>
</value>
<block type="math_number" id="fxt1,y=j(+64nY;~Ksnk">
<field name="NUM">41</field>
</block>
</value>
<block type="math_number" id="K6%o\$-(Zg+9gt:ZeF=T!">
<field name="NUM">48</field>
</block>
</value>
<block type="math_number" id="9*vSgp)3B\$(%[XT@qG[q">
<field name="NUM">49</field>
</block>
</value>
<block type="math_number" id="RK6b:H,5l9)]~-Y@t`z`">
<field name="NUM">71</field>
</block>
</value>
<block type="math_number" id="vwMR[9nP!_xO6*ijgi^D">
<field name="NUM">63</field>
</block>
</value>
<block type="math_number" id="`a}?-e%j2_GCdrJXH?/p">
<field name="NUM">53</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" id="eEA^ill8i(]ajEPhH9:y">
<field name="VAR">intMaxMonthlyRainfall</field>
<value name="VALUE">
<block type="math_on_list" id="GoK0A6c;++F%s%#UTRDi">
<mutation op="MAX"></mutation>
<field name="OP">MAX</field>
<value name="LIST">
<block type="variables_get" id="`B@eXyjV6QWQZ{C/ya#)">
<field name="VAR">intRainfallArray</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" id="ldanr%P+h5`:3kW0mP{S">
<field name="VAR">intMinMonthlyRainfall</field>
<value name="VALUE">
<block type="math_on_list" id="F@GUv3gozHh@^lvYc)sa">
<mutation op="MIN"></mutation>
<field name="OP">MIN</field>
<value name="LIST">
<block type="variables_get" id=".1`?5_iNo(MFY[A0#xuL">
<field name="VAR">intRainfallArray</field>
</block>
</value>
</block>
</value>
<next>
<block type="text_print" id="bn2vR*:]MOCVn|j.ZyZC">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="Fl7|f^|sXU5(#|~Zc(6(">
<mutation items="5"></mutation>
<block type="text" id="RRK])7-SSEu?+0.%Wqn7">
<field name="TEXT">The maximum monthly rainfall (in mm) is </field>
</block>
</value>
<block type="variables_get" id="VrDyzipa{Oz88%.,xwt%">
<field name="VAR">intMaxMonthlyRainfall</field>
</block>
</value>
<block type="text" id="ted-0AN\$=o9L?ndXXV[f">
<field name="TEXT"> and the minimum  is </field>
</block>
</value>
<block type="variables_get" id="S5aa.O8mH:XUGS8Qqz/x">
<field name="VAR">intMinMonthlyRainfall</field>
</block>
</value>
<block type="text" id="8Ms.8b_-UCk:.8qY1+!x">
<field name="TEXT">.</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" id="k;fB4eCEJo1#mNSN}~aR">
<field name="VAR">floatAverageMonthlyRainfall</field>
<value name="VALUE">
<block type="math_arithmetic" id="=\${xq@+b7bsYLI0{FRWI">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block type="math_on_list" id="AJOVsK7iss{uD,M/G6PB">
<mutation op="SUM"></mutation>
<field name="OP">SUM</field>
<value name="LIST">
<block type="variables_get" id="di^V0`){6=o==oGfrQ0B">
<field name="VAR">intRainfallArray</field>
</block>
</value>
</block>
</value>
<value name="B">
<field name="NUM">12</field>
</value>
</block>
</value>
<next>
<block type="text_print" id="/FW8L[W_B-Q8zAH{t--L">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="sRJzEuf@rb;cD!_Wt-UW">
<mutation items="3"></mutation>
<block type="text" id="f^Zk)@woo@U{gE9/hJ8e">
<field name="TEXT">The mean monthly rainfall (in mm) is </field>
</block>
</value>
<block type="procedures_callreturn" id="da*\$8vqc}l8WZeciW^\$y">
<mutation name="RoundTo">
<arg name="floatNum"></arg>
<arg name="intRange"></arg>
</mutation>
<value name="ARG0">
<block type="variables_get" id="IY@Vnru131NvE69seR,N">
<field name="VAR">floatAverageMonthlyRainfall</field>
</block>
</value>
<value name="ARG1">
<block type="math_number" id="K+aHW4JY;i,v]1}pv~y]">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
<block type="text" id="4.Ve2-FLxL!I)e[tn|XU">
<field name="TEXT">.</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</xml>

```

## Generated Smart Pascal Code

```var floatRoundedNum: Float;
var floatPowerOfTen: Float;
var floatNum: Float;
var intRange: Integer;
var floatAverageMonthlyRainfall: Float;
var intRainfallArray: TIntArray;
var intMinMonthlyRainfall: Integer;
var intMaxMonthlyRainfall: Integer;

{
Rounds a number  to intRange decimal places
}
function RoundTo(floatNum: Float; intRange: Integer): Float;
begin
floatPowerOfTen := Power(10,intRange);
floatRoundedNum := Round(floatNum * floatPowerOfTen) / floatPowerOfTen;
Result := floatRoundedNum;
end;

function MaxValueFloat(Arr: TFloatArray): Float;
begin
Result := Arr[0];
for var i := 1 to Arr.Length - 1 do
if Arr[i] > Result then
Result := Arr[i];
end;

function MaxValueInt(Arr: TIntArray): Integer;
begin
Result := Arr[0];
for var i := 1 to Arr.Length - 1 do
if Arr[i] > Result then
Result := Arr[i];
end;

function MinValueFloat(Arr: TFloatArray): Float;
begin
Result := Arr[0];
for var i := 1 to Arr.Length - 1 do
if Arr[i] < Result then
Result := Arr[i];
end;

function MinValueInt(Arr: TIntArray): Integer;
begin
Result := Arr[0];
for var i := 1 to Arr.Length - 1 do
if Arr[i] < Result then
Result := Arr[i];
end;

function Str(v: Variant) : string;
begin
asm
if (@v === 0) {
@Result = '0';
}
else if (@v === '') {
@Result = '';
}
else if (isNaN(@v)) {
@Result = @v;
}
else {
@Result = (@v).toString();
}
end;
end;

function SumFloat(Arr: TFloatArray): Float;
begin
for var i := 0 to Arr.Length - 1 do
Result += Arr[i];
end;

function SumInt(Arr: TIntArray): Integer;
begin
for var i := 0 to Arr.Length - 1 do
Result += Arr[i];
end;

intRainfallArray := [52, 39, 35, 43, 50, 43, 41, 48, 49, 71, 63, 53];
intMaxMonthlyRainfall := MaxValueInt(intRainfallArray);
intMinMonthlyRainfall := MinValueInt(intRainfallArray);
Console.writeln(StrJoin([Str('The maximum monthly rainfall (in mm) is '), Str(intMaxMonthlyRainfall), Str(' and the minimum  is '), Str(intMinMonthlyRainfall), Str('.')],''));
floatAverageMonthlyRainfall := SumInt(intRainfallArray) / 12;
Console.writeln(StrJoin([Str('The mean monthly rainfall (in mm) is '), Str(RoundTo(floatAverageMonthlyRainfall, 2)), Str('.')],''));

```

## Copy of Output

```The maximum monthly rainfall (in mm) is 71 and the minimum  is 35.
The mean monthly rainfall (in mm) is 48.92.```
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion