Product of Array: Difference between revisions
Content deleted Content added
m →[[PHP]]: grammatical changes |
m fixed redirect since multiple redirects aren't followed |
||
(36 intermediate revisions by 25 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
{{task}} |
|||
Compute the product of the elements of an Array |
|||
==[[Ada]]== |
|||
[[Category:Ada]] |
|||
Define an unconstrained array type with an index starting at any valid integer value |
|||
type My_Array is array(Integer range <>) of Integer; |
|||
Define the product function |
|||
function Product(Item : My_Array) return Integer is |
|||
Prod : Integer := 1; |
|||
begin |
|||
for I in Item'range loop |
|||
Prod := Prod * Item(I); |
|||
end loop; |
|||
return Prod; |
|||
end Product; |
|||
This function will raise the pre-defined exception Constraint_Error if the product overflows the values represented by type Integer |
|||
==[[AppleScript]]== |
|||
[[Category:AppleScript]] |
|||
set array to {1, 2, 3, 4, 5} |
|||
set product to 1 |
|||
repeat with i in array |
|||
-- very important -- list index starts at 1 not 0 |
|||
set product to product * i |
|||
end repeat |
|||
==[[BASIC]]== |
|||
[[Category:BASIC]] |
|||
10 REM Create an array with some test data in it |
|||
20 DIM ARRAY(5) |
|||
30 FOR I = 1 TO 5: READ ARRAY(I): NEXT I |
|||
40 DATA 1, 2, 3, 4, 5 |
|||
50 REM Find the sum of elements in the array |
|||
60 PROD = 1 |
|||
70 FOR I = 1 TO 5: PROD = PROD * ARRAY(I): NEXT I |
|||
80 PRINT "The product is "; PROD |
|||
==[[C sharp|C#]]== |
|||
[[Category:C sharp|C#]] |
|||
int value = 1; |
|||
int[] arg = { 1,2,3,4,5 }; |
|||
int arg_length = arg.Length; |
|||
for( int i = 0; i < arg_length; i++ ) |
|||
value *= arg[i]; |
|||
Alternate |
|||
int prod = 1; |
|||
int[] arg = { 1, 2, 3, 4, 5 }; |
|||
foreach (int value in arg) prod *= value; |
|||
==[[C plus plus|C++]]== |
|||
[[Category:C plus plus|C++]] |
|||
int value = 1; |
|||
int arg[] = { 1,2,3,4,5 }; |
|||
int arg_length = sizeof(arg)/sizeof(arg[0]); |
|||
int *end = arg+arg_length; |
|||
for( int *p = arg; p!=end; ++p ) |
|||
value *= *p; |
|||
Using the standard library: |
|||
#include <numeric> |
|||
#include <functional> |
|||
int arg[] = { 1, 2, 3, 4, 5 }; |
|||
int value = std::accumulate(arg, arg+5, 1, std::multiplies<int>()); |
|||
==[[D]]== |
|||
[[Category:D]] |
|||
auto value = 1; |
|||
auto array = [1, 2, 3, 4, 5]; |
|||
foreach(v; array) |
|||
value *= v; |
|||
==[[Forth]]== |
|||
[[Category:Forth]] |
|||
: product ( addr cnt -- n ) |
|||
1 -rot |
|||
cells bounds do i @ * cell +loop ; |
|||
==[[FreeBASIC]]== |
|||
[[Category:FreeBASIC]] |
|||
dim array(4) as integer = { 1, 2, 3, 4, 5 } |
|||
dim product as integer = 1 |
|||
for index as integer = lbound(array) to ubound(array) |
|||
product *= array(index) |
|||
next |
|||
==[[Haskell]]== |
|||
[[Category:Haskell]] |
|||
let x = [1..10] |
|||
product x -- the easy way |
|||
foldl (*) 1 x -- the hard way |
|||
==[[IDL]]== |
|||
[[Category:IDL]] |
|||
array = [3,6,8] |
|||
print,product(array) |
|||
==[[Java]]== |
|||
[[Category:Java]] |
|||
int[] arg = { 1,2,3,4,5 }; |
|||
int value = 1; |
|||
for (int i: arg) |
|||
value *= arg; |
|||
==[[JavaScript]]== |
|||
[[Category:JavaScript]] |
|||
'''Interpreter:''' Firefox 2.0 |
|||
var array = [1, 2, 3, 4, 5]; |
|||
var product = 1; |
|||
for( var i in array ) { |
|||
product *= array[i]; |
|||
} |
|||
alert( product ); |
|||
==[[LISP]]== |
|||
[[Category:LISP]] |
|||
'''Interpreter:''' XEmacs (beta) version 21.5.21 of February 2005 (+CVS-20050720) |
|||
(setq array [1 2 3 4 5]) |
|||
(eval (concatenate 'list '(*) array)) |
|||
'''Interpreter:''' CLisp (ANSI Common Lisp) |
|||
(setq data '(1 2 3 4 5)) |
|||
(apply #'* data) |
|||
==[[OCaml]]== |
|||
[[Category:OCaml]] |
|||
let x = [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|];; |
|||
Array.fold_left ( * ) 1 x |
|||
Variant, using a liked list rather than an array: |
|||
let x = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];; |
|||
List.fold_left ( * ) 1 x |
|||
==[[PHP]]== |
|||
[[Category:PHP]] |
|||
$array = array(1, 2, 3); |
|||
$product = 1; |
|||
foreach($array as $key) |
|||
{ |
|||
$product *= $key; |
|||
} |
|||
echo($product); |
|||
There is also an array product function built-in to PHP: |
|||
$array = array(1,2,3); |
|||
echo array_product($array); |
|||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
'''Interpeter:''' [[Perl]] |
|||
my $var; |
|||
my @list = (1, 2, 3); |
|||
map($var *= $_, @list); |
|||
Alternate |
|||
my $var; |
|||
my @list = (1, 2, 3); |
|||
$var *= $_ for (@list); |
|||
==[[Prolog]]== |
|||
[[Category:Prolog]] |
|||
product([],1). |
|||
product([H|T],X) :- product(T,Y), X is H * X. |
|||
test |
|||
:- product([1,2,3,4,5],X). |
|||
X = 120; |
|||
==[[Python]]== |
|||
[[Category:Python]] |
|||
'''Interpreter:''' [[Python]] 2.5 |
|||
x = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|||
product = 1 |
|||
for i in x: |
|||
product *= i |
|||
or |
|||
reduce(lambda z, y: z * y, x) |
|||
'''Note:''' It is encouraged not to use this as reduce may go away in the future. |
|||
==[[Ruby]]== |
|||
[[Category:Ruby]] |
|||
ary = [1,2,3,4,5] |
|||
# or ary = *1..5 |
|||
product = ary.inject{ |currentProduct, element| currentProduct * element } |
|||
# => 120 |
|||
== [[Scala]] == |
|||
[[Category:Scala]] |
|||
val a = Array(1,2,3,4,5) |
|||
a.foldLeft(1){(x,y) => x * y} |
|||
It may also be done in a classic imperative way : |
|||
var product = 1 |
|||
for (val x <- a) product = product * x |
|||
==[[Scheme]]== |
|||
[[Category:Scheme]] |
|||
(define x '(1 2 3 4 5)) |
|||
(apply * x) |
|||
A recursive solution, without the n-ary operator "trick": |
|||
(define (reduce f u L) |
|||
(if (null? L) |
|||
u |
|||
(f (car L) (reduce f u (cdr L))))) |
|||
(reduce * 1 '(1 2 3 4 5)) ;; 1 is unit for * |
|||
==[[Standard ML]]== |
|||
[[Category:Standard ML]] |
|||
val x = [1,2,3,4,5] |
|||
foldl (op*) 1 x |
|||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
set arr [list 3 6 8] |
|||
set result [expr [join $arr *]] |
Latest revision as of 08:39, 2 July 2010
Redirect to: