Sum and product of an array: Difference between revisions

Content added Content deleted
Line 17: Line 17:
type Int_Array is array(Integer range <>) of Integer;
type Int_Array is array(Integer range <>) of Integer;


array : Int_Arrayr := (1,2,3,4,5,6,7,8,9,10);
array : Int_Array := (1,2,3,4,5,6,7,8,9,10);
Sum : Integer := 0;
Sum : Integer := 0;
for I in array'range loop
for I in array'range loop
Line 33: Line 33:
end Product;
end Product;
This function will raise the predefined exception Constraint_Error if the product overflows the values represented by type Integer
This function will raise the predefined exception Constraint_Error if the product overflows the values represented by type Integer
=={{header|ALGOL 68}}==
main:(
INT default upb := 3;
MODE INTARRAY = [default upb]INT;
INTARRAY array = (1,2,3,4,5,6,7,8,9,10);
INT sum := 0;
FOR i FROM LWB array TO UPB array DO
sum +:= array[i]
OD;
# Define the product function #
PROC int product = (INTARRAY item)INT:
(
INT prod :=1;
FOR i FROM LWB item TO UPB item DO
prod *:= item[i]
OD;
prod
) # int product # ;
printf(($" Sum: "g(0)$,sum,$", Product:"g(0)";"l$,int product(array)))
)
Output:
Sum: 55, Product:3628800;


=={{header|AppleScript}}==
=={{header|AppleScript}}==