How to resolve the algorithm Range expansion step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range expansion step by step in the PL/I programming language

Table of Contents

Problem Statement

A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).

Expand the range description: Note that the second element above, is the range from minus 3 to minus 1.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range expansion step by step in the PL/I programming language

Source code in the pl/i programming language

range_expansion:
   procedure options (main);

get_number:
   procedure (Number, c, eof);
   declare number fixed binary (31), c character (1), eof bit (1) aligned;
   declare neg fixed binary (1);

   number = 0; eof = false;
   do until (c ^= ' ');
      get edit (c) (a(1));
   end;
   if c = '-' then do; get edit (c) (a(1)); neg = -1; end; else neg = 1;
   do forever;
      select (c);
         when ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
                    number = number*10 + c;
         when (',', '-') do; number = neg*number; return; end;
         otherwise signal error;
      end;
      on endfile (sysin) go to exit;
      get edit (c) (a(1));
   end;
exit:
   number = neg*number;
   eof = true;
end get_Number;

   declare c character, (i, range_start, range_end) fixed binary (31);
   declare eof bit (1) aligned;
   declare true bit (1) value ('1'b), false bit (1) value ('0'b);
   declare delimiter character (1) initial (' ');
   declare out file output;

   open file (out) output title ('/out, type(text),recsize(80)');
   do while (^eof);
      call get_number(range_start, c, eof);
      if c = '-' then /* we have a range */
         do;
            call get_number (range_end, c, eof);
            do i = range_start to range_end;
               put file (out) edit (delimiter, i) (a, f(3));
            end;
         end;
      else
         do;
            put file (out) edit (delimiter, range_start) (a, f(3));
         end;
      delimiter = ',';
   end;
end range_expansion;

  

You may also check:How to resolve the algorithm Hailstone sequence step by step in the PL/I programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the PL/I programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the PL/I programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the PL/I programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the PL/I programming language