How to resolve the algorithm Department numbers step by step in the ALGOL W programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Department numbers step by step in the ALGOL W programming language

Table of Contents

Problem Statement

There is a highly organized city that has decided to assign a number to each of their departments:

Each department can have a number between   1   and   7   (inclusive). The three department numbers are to be unique (different from each other) and must add up to   12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.

Write a computer program which outputs all valid combinations.

Possible output   (for the 1st and 14th solutions):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Department numbers step by step in the ALGOL W programming language

Source code in the algol programming language

begin
    % show possible department number allocations for police, sanitation and fire departments %
    % the police department number must be even, all department numbers in the range 1 .. 7   %
    % the sum of the department numbers must be 12                                            %
    integer MAX_DEPARTMENT_NUMBER, DEPARTMENT_SUM;
    MAX_DEPARTMENT_NUMBER :=  7;
    DEPARTMENT_SUM        := 12;
    write( "police sanitation fire" );
    for police := 2 step 2  until MAX_DEPARTMENT_NUMBER do begin
        for sanitation := 1 until MAX_DEPARTMENT_NUMBER do begin
            IF sanitation not = police then begin
                integer fire;
                fire := ( DEPARTMENT_SUM - police ) - sanitation;
                if  fire > 0 and fire <= MAX_DEPARTMENT_NUMBER and fire not = sanitation and fire not = police then begin
                    write( s_w := 0, i_w := 6, police, i_w := 11, sanitation, i_w := 5, fire )
                end if_valid_combination
            end if_sanitation_ne_police
        end for_sanitation
    end for_police
end.

  

You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the D programming language
You may also check:How to resolve the algorithm Joystick position step by step in the C programming language
You may also check:How to resolve the algorithm Rosetta Code/Find unimplemented tasks step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the R programming language
You may also check:How to resolve the algorithm Empty program step by step in the FunL programming language