1
   IDENTIFICATION DIVISION.
   PROGRAM-ID. HELLO-WORLD.
   DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NAME PIC A(9) VALUE 'FELIX'.

is there other way of looping?

   PROCEDURE DIVISION.
       PERFORM A-PARA THRU E-PARA.

   A-PARA.
   DISPLAY 'L'.

   B-PARA.
   DISPLAY 'I I I'.

   C-PARA.
   DISPLAY 'A A A A A'.

   D-PARA.
   DISPLAY 'M M M M M M M'.

  This is the Output:
  L
  III
  AAAAA
  MMMMMMM

  My expecting Output Should be:
      L
     III
    AAAAA
   MMMMMMM

thanks for the response in advance. I'm new to this programing language and its very confusing.

  • For the "loop": check out `PERFORM VARYING` (you could move the data into a table with `OCCURS` so the varying can provide you with the index, or use a big variable and reference modification (with increasing size [`BY 2`]): if you inspect leading spaces - then just add them. – Simon Sobisch Dec 11 '21 at 20:41
  • check this answer [maybe you will find this useful](https://stackoverflow.com/questions/52437225/i-want-to-print-triangle-of-on-middle-of-the-screen-using-cobol-how-do-i/52443316) – Roie Dec 12 '21 at 19:02
  • check this answer: [maybe you will find it useful](https://stackoverflow.com/questions/52437225/i-want-to-print-triangle-of-on-middle-of-the-screen-using-cobol-how-do-i/52443316) – Roie Dec 12 '21 at 19:04
  • check this answer, maybe it will be useful for you. [pyramid](https://stackoverflow.com/questions/52437225/i-want-to-print-triangle-of-on-middle-of-the-screen-using-cobol-how-do-i/52443316) – Roie Dec 12 '21 at 19:11

3 Answers3

1

Really shouldn't ask people to do your homework . . .

I did this on an openvms machine. You will have to tweak for whatever COBOL environment you are using.

$ cob/reserved_words=200x PYRAMID.COB
$ LINK PYRAMID
$ RUN PYRAMID
    *     
   ***    
  *****   
 *******  
********* 

You can get yourself a free account in the same place if you have access to a good VT-100 emulator.

https://eisner.decus.org/

IDENTIFICATION DIVISION.
PROGRAM-ID.    A.

*>  COB/RESERVED_WORDS=200X PYRAMID.COB
*>  LINK PYRAMID
*>  RUN PYRAMID

DATA DIVISION.
     WORKING-STORAGE SECTION.
     

     01 MY-NUMBERS.
        05 START-POS    PIC 9(4) USAGE IS COMP VALUE IS ZERO.
        05 END-POS      PIC 9(4) USAGE IS COMP VALUE IS ZERO.
        05 SUB-POS      PIC 9(4) USAGE IS COMP.
        05 DISTANCE     PIC 9(4) USAGE IS COMP.


     77 MAX-WIDTH       PIC 9(4) USAGE IS COMP VALUE IS 10.
     77 CENTER-POS      PIC 9(4) USAGE IS COMP VALUE IS 5.

     01 MY-DISPLAYS.
        05 DISPLAY-LINE.
           10 DISPLAY-CELL   PIC X OCCURS 10.


PROCEDURE DIVISION.
A000-MAIN.

     PERFORM B000-CREATE-PYRAMID
        VARYING DISTANCE FROM 0 BY 1
        UNTIL DISTANCE IS GREATER THAN 4.

    STOP RUN.

B000-CREATE-PYRAMID.
    MOVE SPACES TO DISPLAY-LINE.

    SUBTRACT DISTANCE FROM CENTER-POS GIVING START-POS.
    ADD DISTANCE TO CENTER-POS GIVING END-POS.

    PERFORM C000-FILL-LINE
        VARYING SUB-POS FROM START-POS BY 1
        UNTIL SUB-POS IS GREATER THAN END-POS.

    DISPLAY DISPLAY-LINE.

C000-FILL-LINE.
    MOVE '*' TO DISPLAY-CELL (SUB-POS).
user3450148
  • 851
  • 5
  • 13
-1

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.
Rick Smith
  • 3,962
  • 6
  • 13
  • 24
-3

there are several ways to loop in cobol

  1. go to how to use GO TO in COBOL (I wouldn't recommend to use)
  2. perform statements https://www.tutorialspoint.com/cobol/cobol_loop_statements.htm
Luke
  • 3
  • 2