Product of Array: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[Toka]]: Updated to work with Toka R1)
m (fixed redirect since multiple redirects aren't followed)
 
(23 intermediate revisions by 13 users not shown)
Line 1: Line 1:
#REDIRECT [[Sum and product of an array]]
{{task}}

Compute the product of the elements of an Array

==[[4D]]==
[[Category:4D]]

ARRAY INTEGER($var;0)
For ($i;1;3)
APPEND TO ARRAY($var;$i)
End for
$product:=1
For ($i;1;Size of array($var))
$product:=$product*$var{$i}
End for

==[[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>());

==[[Clean]]==
[[Category:Clean]]
array = {1, 2, 3, 4, 5}
Start = foldl (*) 1 [x \\ x <-: array]

==[[D]]==
[[Category:D]]

auto value = 1;
auto array = [1, 2, 3, 4, 5];
foreach(v; array)
value *= v;

==[[Erlang]]==
[[Category:Erlang]]

List = [1, 2, 3, 4, 5].
Product = lists:foldl(fun (X, P) -> X * P end, 1, List).

==[[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 *= i;

==[[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)

(defparameter data #1A(1 2 3 4 5))
(reduce #'* 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);

==[[Pop11]]==
[[Category:Pop11]]

Simple loop:
lvars i, prod = 1, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
ar(i)*prod -> prod;
endfor;

==[[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)

or

from operator import mul
reduce(mul, 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)
val product = a.foldLeft(1)(_ * _)
// (_ * _) is a shortcut for {(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 *

==[[Seed7]]==
[[Category:Seed7]]

const func integer: prodArray (in array integer: valueArray) is func
result
var integer: prod is 1;
local
var integer: value is 0;
begin
for value range valueArray do
prod *:= value;
end for;
end func;

Call this function with:

writeln(prodArray([](1, 2, 3, 4, 5)));

==[[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 *]]

==[[Toka]]==
[[Category:Toka]]

4 cells is-array foo
212 1 foo array.put
51 2 foo array.put
12 3 foo array.put
91 4 foo array.put
reset 1 4 0 [ i foo array.get * ] countedLoop .

Latest revision as of 08:39, 2 July 2010