To make a character-based pyramid in COBOL, the first thing to determine is the width of the base of the pyramid. This depends on the number of levels and the "slope" of the sides. For Example 1, at a slope of one character per line, the base will be (2 * (number-of-levels - 1)) + 1
, or (2 * (4 - 1)) + 1 = 7
for the width of the base.
Next find the starting position (mid-point) for the top of the pyramid. This is (width-of-base + 1) / 2
. For Example 1, (7 + 1) / 2 = 4
.
For each level (PERFORM VARYING
) of the pyramid, decrease the starting position by 1 and increase the width by 2.
For Example 2, a "slope" of two characters per line is used. The base is 13, the starting position is 7, the starting position is decreased by 2 (the "slope") and the width is increased by 4.
For both Examples 1 and 2, asterisks (*
or *
) are inserted where the intended characters are to be placed, then an INSPECT
statement is used to change the asterisks to the correct characters. INSPECT
is also a "loop" statement (internally).
For Example 3, a pyramid-like structure, with a "slope" of one, is built from text. The number of levels, the width and starting point are calculated based on the length of the text to be displayed.
Code:
data division.
working-storage section.
1 pyramid-chars pic x(4) value "LIAM".
1 level-number comp pic 9(4).
1 start-point comp pic 9(4).
1 width comp pic 9(4).
1 display-line pic x(50).
1 number-of-levels comp pic 9(4).
1 max-move comp pic 9(4).
1 text-ptr comp pic 9(4).
1 para-text pic x(116) value "This is a long paragraph to "
& "demonstrate building a pyramid from text. The only "
& "explicit loop is the PERFORM VARYING.".
procedure division.
* Example 1
move space to display-line
move 4 to start-point
move 1 to width
perform varying level-number from 1 by 1
until level-number > 4
move all "*" to display-line (start-point:width)
inspect display-line replacing
all "*" by pyramid-chars (level-number:1)
subtract 1 from start-point
add 2 to width
display display-line
end-perform
* Example 2
display space
move space to display-line
move 7 to start-point
move 1 to width
perform varying level-number from 1 by 1
until level-number > 4
move all "* " to display-line (start-point:width)
inspect display-line replacing
all "*" by pyramid-chars (level-number:1)
subtract 2 from start-point
add 4 to width
display display-line
end-perform
* Example 3
display space
compute number-of-levels = ( function sqrt
( 16 * function length (para-text) ) ) / 4
if (number-of-levels *
(2 + 2 * (number-of-levels - 1))) / 2
< function length (para-text)
add 1 to number-of-levels
end-if
compute start-point =
( 2 * (number-of-levels - 1) + 1 ) / 2 + 1
move 1 to width
move 1 to text-ptr
move space to display-line
perform varying level-number from 1 by 1
until level-number > number-of-levels
compute max-move = function min ( (function length
(para-text) - text-ptr + 1) width )
move para-text (text-ptr:max-move)
to display-line (start-point:width)
add max-move to text-ptr
subtract 1 from start-point
add 2 to width
display display-line
end-perform
stop run
.
Output:
L
III
AAAAA
MMMMMMM
L
I I I
A A A A A
M M M M M M M
T
his
is a
long p
aragraph
to demonstr
ate building
a pyramid from
text. The only ex
plicit loop is the
PERFORM VARYING.