RosettaCodeData/Task/Equilibrium-index/Loglan82/equilibrium-index.loglan82
2026-04-30 12:34:36 -04:00

88 lines
2.1 KiB
Text

program equilibrium;
(* Equilibrium index *)
var
x1, x2, x3, x4: arrayof integer,
x1_eqs, x2_eqs, x3_eqs, x4_eqs: arrayof boolean;
unit write_array: procedure (nums: arrayof integer; nl: boolean);
var
i: integer;
begin
for i := lower(nums) to upper(nums)
do
write(nums(i): 1, " ")
od;
if nl then writeln fi
end write_array;
unit write_inds_of_true: procedure (bools: arrayof boolean; nl: boolean);
var
i: integer;
begin
for i := lower(bools) to upper(bools)
do
if bools(i) then write(i: 1, " ") fi;
od;
if nl then writeln fi
end write_inds_of_true;
unit get_inds: procedure (nums: arrayof integer; bools: arrayof boolean);
var
left_sum, right_sum: integer,
i: integer;
begin
left_sum, right_sum := 0;
for i := lower(nums) to upper(nums)
do
right_sum := right_sum + nums(i)
od;
for i := lower(nums) to upper(nums)
do
right_sum := right_sum - nums(i);
bools(i) := (left_sum = right_sum);
left_sum := left_sum + nums(i);
od
end get_inds;
begin
array x1 dim (0 : 6);
array x1_eqs dim (0 : 6);
x1(0) := -7; x1(1) := 1; x1(2) := 5; x1(3) := 2;
x1(4) := -4; x1(5) := 3; x1(6) := 0;
call get_inds(x1, x1_eqs);
array x2 dim (0 : 2);
array x2_eqs dim (0 : 2);
x2(0) := 2; x2(1) := 4; x2(2) := 6;
call get_inds(x2, x2_eqs);
array x3 dim (0 : 2);
array x3_eqs dim (0 : 2);
x3(0) := 2; x3(1) := 9; x3(2) := 2;
call get_inds(x3, x3_eqs);
array x4 dim (0 : 6);
array x4_eqs dim (0 : 6);
x4(0) := 1; x4(1) := -1; x4(2) := 1; x4(3) := -1;
x4(4) := 1; x4(5) := -1; x4(6) := 1;
call get_inds(x4, x4_eqs);
writeln("Results:");
writeln;
write("X1: ");
call write_array(x1, true);
write("Eqs: ");
call write_inds_of_true(x1_eqs, true);
writeln;
write("X2: ");
call write_array(x2, true);
write("Eqs: ");
call write_inds_of_true(x2_eqs, true);
writeln;
write("X3: ");
call write_array(x3, true);
write("Eqs: ");
call write_inds_of_true(x3_eqs, true);
writeln;
write("X4: ");
call write_array(x4, true);
write("Eqs: ");
call write_inds_of_true(x4_eqs, true);
end equilibrium.