Advertisement
Miquel_Fuster

Temperature average

Aug 31st, 2022 (edited)
2,763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
COBOL 2.52 KB | None | 0 0
  1. *> Display the average of the five highest temperatures for five cities.
  2. *> GNU-COBOL
  3. *> Free format
  4.  
  5. PROGRAM-ID. TEMPERATURE.
  6. DATA DIVISION.
  7. WORKING-STORAGE SECTION.
  8. 01 CITY                         OCCURS 5 TIMES INDEXED BY CITY-ID.
  9.     05 CITY-NAME    PIC X(10).
  10.     05 CITY-TEMP    PIC 99V9    OCCURS 5 TIMES INDEXED BY TEMP-ID.
  11. 77 COUNTER          PIC 9.
  12. 77 AVG-CITY-TEMP    PIC 99.9    OCCURS 5 TIMES.
  13. 77 SUM-OF-TEMP      PIC 999V9.
  14.  
  15. SCREEN SECTION.
  16. 01 CLEAR-SCREEN.
  17.     05 BLANK SCREEN.
  18.    
  19. 01 CITIES-DATA.
  20.     05 VALUE "Enter the last 5 daily hight temperature for:" LINE 1 COL 1.
  21.     05 CITY-NAME-INPUT COL 47 PIC X(10) TO CITY-NAME(CITY-ID) AUTO.
  22.     05 VALUE "Temperatures:" LINE 3 COL 1.
  23.     05 VALUE "Temperature #1: " LINE 4 COL 1.
  24.     05 TEMP1_INPUT PIC 99.9 COL 17 TO CITY-TEMP(CITY-ID, 1) AUTO.
  25.     05 VALUE "Temperature #2: " LINE 5 COL 1.
  26.     05 TEMP2_INPUT PIC 99.9 COL 17 TO CITY-TEMP(CITY-ID, 2) AUTO.
  27.     05 VALUE "Temperature #3: " LINE 6 COL 1.
  28.     05 TEMP3_INPUT PIC 99.9 COL 17 TO CITY-TEMP(CITY-ID, 3) AUTO.
  29.     05 VALUE "Temperature #4: " LINE 7 COL 1.
  30.     05 TEMP4_INPUT PIC 99.9 COL 17 TO CITY-TEMP(CITY-ID, 4) AUTO.
  31.     05 VALUE "Temperature #5: " LINE 8 COL 1.
  32.     05 TEMP5_INPUT PIC 99.9 COL 17 TO CITY-TEMP(CITY-ID, 5).
  33.  
  34. 01 CITIES-DISPLAY BLANK SCREEN.
  35.     05 VALUE "Here are the average temperatures for cities in the area" LINE 1 COL 1.
  36.     05 FROM CITY-NAME(1) PIC X(10) LINE 3 COL 10.
  37.     05 FROM CITY-NAME(2) PIC X(10) COL 20.
  38.     05 FROM CITY-NAME(3) PIC X(10) COL 30.
  39.     05 FROM CITY-NAME(4) PIC X(10) COL 40.
  40.     05 FROM CITY-NAME(5) PIC X(10) COL 50.
  41.    
  42.     05 FROM AVG-CITY-TEMP(1) PIC 99.9 LINE 4 COL 10.
  43.     05 VALUE "C" COL 15.
  44.     05 FROM AVG-CITY-TEMP(2) PIC 99.9 COL 20.
  45.     05 VALUE "C" COL 25.
  46.     05 FROM AVG-CITY-TEMP(3) PIC 99.9 COL 30.
  47.     05 VALUE "C" COL 35.
  48.     05 FROM AVG-CITY-TEMP(4) PIC 99.9 COL 40.
  49.     05 VALUE "C" COL 45.
  50.     05 FROM AVG-CITY-TEMP(5) PIC 99.9 COL 50.
  51.     05 VALUE "C" COL 55.
  52.    
  53. PROCEDURE DIVISION.
  54. P1.
  55.     PERFORM P-CITY-LOOP VARYING CITY-ID FROM 1 BY 1 UNTIL CITY-ID > 5
  56.     DISPLAY CLEAR-SCREEN
  57.     DISPLAY CITIES-DISPLAY
  58.     GOBACK.
  59.    
  60. P-CITY-LOOP.
  61.     INITIALIZE CITIES-DATA
  62.     DISPLAY CITIES-DATA
  63.     ACCEPT CITIES-DATA
  64.     PERFORM CALCULATE-TEMP.
  65.    
  66. CALCULATE-TEMP.
  67.     INITIALIZE SUM-OF-TEMP
  68.     PERFORM VARYING TEMP-ID FROM 1 BY 1 UNTIL TEMP-ID > 5
  69.         ADD CITY-TEMP(CITY-ID, TEMP-ID) TO SUM-OF-TEMP
  70.     END-PERFORM
  71.     DIVIDE SUM-OF-TEMP BY 5 GIVING AVG-CITY-TEMP(CITY-ID).
  72.    
  73. END PROGRAM TEMPERATURE.
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement