Sum and product of an array: Difference between revisions
→{{header|langur}}
VincentARM (talk | contribs) (add task to arm assembly raspberry pi) |
Langurmonkey (talk | contribs) |
||
(9 intermediate revisions by 7 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 785 ⟶ 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 1,224 ⟶ 1,383:
=={{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
<- ` '''formal''' array;
'''new''' sum; '''new''' product; '''new''' i; '''new''' v; '''label''' arrayLoop;
arrayLoop: '''if''' [ i
'''end''' '''else'''
'''new''' sp;
sp <- sumAndProduct( ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) );
'''out''' sp[
'''end'''
'''end''' $
=={{header|Euphoria}}==
Line 1,427 ⟶ 1,584:
=={{header|FutureBasic}}==
Traditional
<syntaxhighlight lang="futurebasic">
local fn Sum( mutArr as CFMutableArrayRef ) as float
Line 1,453 ⟶ 1,611:
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}}
'''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 1,888 ⟶ 2,083:
=={{header|langur}}==
<syntaxhighlight lang="langur">val .
writeln "
writeln " sum: ", fold
writeln "product: ", fold
</syntaxhighlight>
{{out}}
<pre>
sum: 190
product: 121645100408832000</pre>
Line 3,547 ⟶ 3,737:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="
var a = [7, 10, 2, 4, 6, 1, 8, 3, 9, 5]
System.print("Array : %(a)")
|