Sum and product of an array: Difference between revisions

 
(7 intermediate revisions by 6 users not shown)
Line 934:
{{Out}}
<pre>77.9699690</pre>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="eulerbruijn">
:import std/List .
:import std/Math .
 
arr (+1) : ((+2) : ((+3) : {}(+4)))
 
main [∑arr : ∏arr]
</syntaxhighlight>
 
=={{header|C}}==
Line 1,373 ⟶ 1,383:
=={{header|Euler}}==
In Euler, a list must be assigned to a variable in order for it to be subscripted.
'''begin'''
<syntaxhighlight lang="euler">
'''new''' sumAndProduct;
begin
'''new''' sumField; '''new''' productField;
new sumAndProduct;
sumAndProduct
new sumField; new productField;
&lt;- ` '''formal''' array;
sumAndProduct
<- ` formal array; '''begin'''
'''new''' sum; '''new''' product; '''new''' i; '''new''' v; '''label''' arrayLoop;
begin
new sum;v new product; new i; new v &lt;- label arrayLooparray;
v sum <&lt;- array0;
sum product <&lt;- 01;
product <i &lt;- 10;
arrayLoop: '''if''' [ i &lt;- i + 1 ] &lt;= '''length''' array <-'''then''' 0;'''begin'''
arrayLoop: if [ i < sum &lt;- i + 1 ] <=sum length+ arrayv[ theni begin];
sum product <&lt;- product sum +* v[ i ];
product <-'''goto''' product * v[ i ];arrayLoop
'''end''' '''else''' goto arrayLoop0;
end elsesumField 0 &lt;- 1;
sumField productField <&lt;- 12;
productField <-( 2;sum, product )
( sum, product )'''end'''
end&apos;;
';''begin'''
'''new''' sp;
begin
sp &lt;- sumAndProduct( ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) );
new sp;
sp <-'''out''' sumAndProduct( ( 1, 2, 3, 4, 5, 6,sp[ 7,sumField 8, 9, 10 ) )];
'''out''' sp[ sumField productField ];
'''end'''
out sp[ productField ]
'''end''' $
end
end $
</syntaxhighlight>
 
=={{header|Euphoria}}==
Line 1,576 ⟶ 1,584:
 
=={{header|FutureBasic}}==
Traditional
<syntaxhighlight lang="futurebasic">
local fn Sum( mutArr as CFMutableArrayRef ) as float
Line 1,602 ⟶ 1,611:
end fn = prod
</syntaxhighlight>
Sum of array elements with key-value coding
<syntaxhighlight lang="langurfuturebasic">val .arr = series 19
local fn NumericalArraySum( array as CFArrayRef ) as CFNumberRef
end fn = fn ObjectValueForKeyPath( array, @"@sum.self" )
 
printf @"%@", fn NumericalArraySum( @[@0.0454, @-1.3534, @0.345, @65, @-0.345, @1.35] )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
65.042
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_and_product_of_an_array}}
 
 
'''Solution'''
 
[[File:Fōrmulæ - Sum and product of an array 01.png]]
 
 
'''Test cases'''
 
[[File:Fōrmulæ - Sum and product of an array 02.png]]
 
[[File:Fōrmulæ - Sum and product of an array 03.png]]
 
 
[[File:Fōrmulæ - Sum and product of an array 04.png]]
 
[[File:Fōrmulæ - Sum and product of an array 05.png]]
 
 
[[File:Fōrmulæ - Sum and product of an array 06.png]]
 
[[File:Fōrmulæ - Sum and product of an array 07.png]]
 
 
[[Wp:Empty sum|Empty sum]] and [[Wp:Empty_product|empty product]]:
 
[[File:Fōrmulæ - Sum and product of an array 08.png]]
 
[[File:Fōrmulæ - Sum and product of an array 09.png]]
 
=={{header|Gambas}}==
Line 2,033 ⟶ 2,083:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .arrlist = series 19
writeln " array list: ", .arrlist
writeln " sum: ", fold f .x fn{+ .y}, .arrlist
writeln "product: ", fold f .x x .yfn{*}, .arr</syntaxhighlight>list
</syntaxhighlight>
 
{{works with|langur|0.6.6}}
<syntaxhighlight lang="langur">val .arr = series 19
writeln " array: ", .arr
writeln " sum: ", fold f{+}, .arr
writeln "product: ", fold f{x}, .arr</syntaxhighlight>
 
{{out}}
<pre> array list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
sum: 190
product: 121645100408832000</pre>
Line 3,692 ⟶ 3,737:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Nums
var a = [7, 10, 2, 4, 6, 1, 8, 3, 9, 5]
System.print("Array : %(a)")
885

edits