Sum and product of an array: Difference between revisions

(add task to arm assembly raspberry pi)
(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'''
<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,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}}
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,888 ⟶ 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,547 ⟶ 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)")
973

edits