Sum and product of an array: Difference between revisions

m (syntax highlighting fixup automation)
 
(27 intermediate revisions by 18 users not shown)
Line 61:
$sum:=sum($list)
</syntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program sumandproduct64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSum: .asciz "Sum = "
szMessProd: .asciz "Product = "
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
szMessErreur: .asciz "Overflow ! \n"
 
tabArray: .quad 2, 11, 19, 90, 55,1000000
.equ TABARRAYSIZE, (. - tabArray) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
ldr x2,qAdrtabArray
mov x1,#0 // indice
mov x0,#0 // sum init
1:
ldr x3,[x2,x1,lsl #3]
adds x0,x0,x3
bcs 99f
add x1,x1,#1
cmp x1,#TABARRAYSIZE
blt 1b
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
mov x0,#3 // number string to display
ldr x1,qAdrszMessSum
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
ldr x2,qAdrtabArray
mov x1,#0 // indice
mov x0,#1 // product init
2:
ldr x3,[x2,x1,lsl #3]
mul x0,x3,x0
umulh x4,x3,x0
cmp x4,#0
bne 99f
add x1,x1,#1
cmp x1,#TABARRAYSIZE
blt 2b
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
mov x0,#3 // number string to display
ldr x1,qAdrszMessProd
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
b 100f
99:
ldr x0,qAdrszMessErreur
bl affichageMess
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessSum: .quad szMessSum
qAdrszMessProd: .quad szMessProd
qAdrszMessErreur: .quad szMessErreur
qAdrszMessStart: .quad szMessStart
qAdrtabArray: .quad tabArray
 
/***************************************************/
/* display multi strings */
/* new version 24/05/2023 */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* x4 address string4 */
/* x5 address string5 */
/* x6 address string5 */
displayStrings: // INFO: displayStrings
stp x7,lr,[sp,-16]! // save registers
stp x2,fp,[sp,-16]! // save registers
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes)
mov x7,x0 // save strings number
cmp x7,#0 // 0 string -> end
ble 100f
mov x0,x1 // string 1
bl affichageMess
cmp x7,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x7,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x7,#3
ble 100f
mov x0,x4
bl affichageMess
cmp x7,#4
ble 100f
mov x0,x5
bl affichageMess
cmp x7,#5
ble 100f
mov x0,x6
bl affichageMess
100:
ldp x2,fp,[sp],16 // restaur registers
ldp x7,lr,[sp],16 // restaur registers
ret
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Sum = 1000177
Product = 2069100000000
</pre>
=={{header|ACL2}}==
<syntaxhighlight lang="lisp">(defun sum (xs)
Line 366 ⟶ 515:
{{Out}}
<syntaxhighlight lang="applescript">{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {sum:55}, {product:3628800}}</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program sumandproduct.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSum: .asciz "Sum = "
szMessProd: .asciz "Product = "
szMessStart: .asciz "Program 32 bits start.\n"
szCarriageReturn: .asciz "\n"
szMessErreur: .asciz "Overflow ! \n"
 
tabArray: .int 2, 11, 19, 90, 55,1000
.equ TABARRAYSIZE, (. - tabArray) / 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
ldr r2,iAdrtabArray
mov r1,#0 @ indice
mov r0,#0 @ sum init
1:
ldr r3,[r2,r1,lsl #2]
adds r0,r0,r3
bcs 99f
add r1,r1,#1
cmp r1,#TABARRAYSIZE
blt 1b
ldr r1,iAdrsZoneConv
bl conversion10 @ decimal conversion
mov r3,#0
strb r3,[r1,r0]
mov r0,#3 @ number string to display
ldr r1,iAdrszMessSum
ldr r2,iAdrsZoneConv @ insert conversion in message
ldr r3,iAdrszCarriageReturn
bl displayStrings @ display message
ldr r2,iAdrtabArray
mov r1,#0 @ indice
mov r0,#1 @ product init
2:
ldr r3,[r2,r1,lsl #2]
umull r0,r4,r3,r0
cmp r4,#0
bne 99f
add r1,r1,#1
cmp r1,#TABARRAYSIZE
blt 2b
ldr r1,iAdrsZoneConv
bl conversion10 @ decimal conversion
mov r3,#0
strb r3,[r1,r0]
mov r0,#3 @ number string to display
ldr r1,iAdrszMessProd
ldr r2,iAdrsZoneConv @ insert conversion in message
ldr r3,iAdrszCarriageReturn
bl displayStrings @ display message
b 100f
99:
ldr r0,iAdrszMessErreur
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsZoneConv: .int sZoneConv
iAdrszMessSum: .int szMessSum
iAdrszMessProd: .int szMessProd
iAdrszMessErreur: .int szMessErreur
iAdrszMessStart: .int szMessStart
iAdrtabArray: .int tabArray
 
/***************************************************/
/* display multi strings */
/***************************************************/
/* r0 contains number strings address */
/* r1 address string1 */
/* r2 address string2 */
/* r3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: @ INFO: displayStrings
push {r1-r4,fp,lr} @ save des registres
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes)
mov r4,r0 @ save strings number
cmp r4,#0 @ 0 string -> end
ble 100f
mov r0,r1 @ string 1
bl affichageMess
cmp r4,#1 @ number > 1
ble 100f
mov r0,r2
bl affichageMess
cmp r4,#2
ble 100f
mov r0,r3
bl affichageMess
cmp r4,#3
ble 100f
mov r3,#3
sub r2,r4,#4
1: @ loop extract address string on stack
ldr r0,[fp,r2,lsl #2]
bl affichageMess
subs r2,#1
bge 1b
100:
pop {r1-r4,fp,pc}
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
Sum = 1177
Product = 2069100000
</pre>
 
=={{header|Arturo}}==
Line 512 ⟶ 810:
PRINT "The product is ",product
</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 rem Sum and product of an array
20 dim array(4)' array de 5 eltos.
30 data 1,2,3,4,5
40 for index = 0 to ubound(array)
50 read array(index)
60 next index
70 sum = 0
80 prod = 1
90 for index = 0 to 4 ubound(array)
100 sum = sum+array(index)
110 prod = prod*array(index)
120 next index
130 print "The sum is ";sum
140 print "and the product is ";prod
150 end</syntaxhighlight>
 
==={{header|BBC BASIC}}===
Line 617 ⟶ 934:
{{Out}}
<pre>77.9699690</pre>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/List .
:import std/Math .
 
arr (+1) : ((+2) : ((+3) : {}(+4)))
 
main [∑arr : ∏arr]
</syntaxhighlight>
 
=={{header|C}}==
Line 901 ⟶ 1,228:
accum 0 for x in [1,2,3,4,5] { _ + x }
accum 1 for x in [1,2,3,4,5] { _ * x }</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
array[] = [ 5 1 19 25 12 1 14 7 ]
product = 1
for item in array[]
sum += item
product *= item
.
print "Sum: " & sum
print "Product: " & product</syntaxhighlight>
{{out}}
<pre>
Sum: 84
Product: 2793000
</pre>
 
=={{header|Eiffel}}==
Line 1,037 ⟶ 1,380:
sum_tail([Head|Tail], Acc) ->
sum_tail(Tail, Head + Acc).</syntaxhighlight>
 
=={{header|Euler}}==
In Euler, a list must be assigned to a variable in order for it to be subscripted.
'''begin'''
'''new''' sumAndProduct;
'''new''' sumField; '''new''' productField;
sumAndProduct
&lt;- ` '''formal''' array;
'''begin'''
'''new''' sum; '''new''' product; '''new''' i; '''new''' v; '''label''' arrayLoop;
v &lt;- array;
sum &lt;- 0;
product &lt;- 1;
i &lt;- 0;
arrayLoop: '''if''' [ i &lt;- i + 1 ] &lt;= '''length''' array '''then''' '''begin'''
sum &lt;- sum + v[ i ];
product &lt;- product * v[ i ];
'''goto''' arrayLoop
'''end''' '''else''' 0;
sumField &lt;- 1;
productField &lt;- 2;
( sum, product )
'''end'''
&apos;;
'''begin'''
'''new''' sp;
sp &lt;- sumAndProduct( ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) );
'''out''' sp[ sumField ];
'''out''' sp[ productField ]
'''end'''
'''end''' $
 
=={{header|Euphoria}}==
Line 1,177 ⟶ 1,551:
Sum = 14
Product = 72
</pre>
=={{header|FreePascal}}==
<syntaxhighlight lang="pascal">
program sumproduct;
var
a:array[0..4] of integer =(1,2,3,4,5);
i:integer;
sum :Cardinal = 0;
prod:Cardinal = 1;
begin
for i in a do
sum :=sum+i;
for i in a do
prod:=prod * i;
writeln('sum: ',sum);
writeln('prod:',prod);
end.
</syntaxhighlight>
{{out}}
<pre>
15
120
</pre>
 
Line 1,185 ⟶ 1,581:
product[a]
</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
Traditional
<syntaxhighlight lang="futurebasic">
local fn Sum( mutArr as CFMutableArrayRef ) as float
NSInteger i, count, value = 0
float sum = 0
count = fn ArrayCount( mutArr )
for i = 0 to count -1
value = fn NumberIntegerValue( fn ArrayObjectAtIndex( mutArr, i ) )
sum += value
next
end fn = sum
 
 
local fn Product( mutArr as CFMutableArrayRef ) as float
NSInteger i, count, value = 0
float prod = 0
count = fn ArrayCount( mutArr )
for i = 0 to count -1
value = fn NumberIntegerValue( fn ArrayObjectAtIndex( mutArr, i ) )
prod *= value
next
end fn = prod
</syntaxhighlight>
Sum of array elements with key-value coding
<syntaxhighlight lang="futurebasic">
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}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
 
'''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]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sum and product of an array 09.png]]
In '''[https://formulae.org/?example=Sum_and_product_of_an_array this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
Line 1,215 ⟶ 1,679:
The Product = 120
</pre>
 
 
=={{header|GAP}}==
Line 1,232 ⟶ 1,697:
Product(v, n -> 1/n);
# 1/40320</syntaxhighlight>
 
 
 
=={{header|GFA Basic}}==
Line 1,252 ⟶ 1,719:
PRINT "Product is ";product%
</syntaxhighlight>
 
 
 
=={{header|Go}}==
Line 1,303 ⟶ 1,772:
 
=={{header|GW-BASIC}}==
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
 
<syntaxhighlight lang="qbasic">10 REM Create an array with some test data in it
Line 1,509 ⟶ 1,982:
3628800
]</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">[1 2 3 4 5] 0 [+] fold.</syntaxhighlight>
<syntaxhighlight lang="joy">[1 2 3 4 5] 1 [*] fold.</syntaxhighlight>
 
=={{header|jq}}==
Line 1,586 ⟶ 2,063:
9332621544394415268169923885626670049071596826438162146859296389521759999322991
5608941463976156518286253697920827223758251185210916864000000000000000000000000
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&values = fn.arrayGenerateFrom(fn.inc, 5)
 
fn.println(fn.arrayReduce(&values, 0, fn.add))
# Output: 15
 
fn.println(fn.arrayReduce(&values, 1, fn.mul))
# Output: 120
</syntaxhighlight>
 
Line 1,595 ⟶ 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 2,686 ⟶ 3,169:
See "Product = " + nProduct + nl
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP
DUP 1 CON DOT
SWAP ARRY→ LIST→ SWAP 1 - START * NEXT
'SUMPR' STO
[ 2 4 8 -5 ] SUMPR
{{out}}
<pre>
2: 9
1: -320
</pre>
 
=={{header|Ruby}}==
Line 2,900 ⟶ 3,398:
Prod: 120
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "arraysum" )
@( description, "Compute the sum and product of an array of integers." )
@( see_also, "http://rosettacode.org/wiki/Sum_and_product_of_an_array" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure arraysum is
type int_array is array(1..10) of integer;
myarr : int_array := (1,2,3,4,5,6,7,8,9,10 );
begin
? stats.sum( myarr );
declare
product : integer := 1;
begin
for i in arrays.first( myarr )..arrays.last( myarr ) loop
product := @ * myarr(i);
end loop;
? product;
end;
end arraysum;</syntaxhighlight>
 
=={{header|Sparkling}}==
Line 3,176 ⟶ 3,700:
End Sub
End Module</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
fn main() {
values := [1, 2, 3, 4, 5]
mut sum, mut prod := 0, 1
for val in values {
sum += val
prod *= val
}
println("sum: $sum\nproduct: $prod")
}
</syntaxhighlight>
 
{{out}}
<pre>
sum: 15
product: 120
</pre>
 
=={{header|Wart}}==
Line 3,194 ⟶ 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)")
Line 3,286 ⟶ 3,829:
end</syntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const print = @import("std").debug.print;
pub fn main() void {
const numbers = [_]u8{ 1, 2, 3, 4, 5 };
var sum: u8 = 0;
var product: u8 = 1;
for (numbers) |number| {
product *= number;
sum += number;
}
print("{} {}\n", .{ product, sum });
}</syntaxhighlight>
 
=={{header|zkl}}==
885

edits