CSE 2133 - Lab 1 Part A You will write an interactive program (name it lab1a.cob
ID: 3882774 • Letter: C
Question
CSE 2133 - Lab 1
Part A
You will write an interactive program (name it lab1a.cob) that will prompt the user for a 4 digit (unsigned) number. The program will repeat this until the user enters a 0. Once a 0 is entered by the user the program will stop executing; this program only performs a single session.
When the user enters a 0, the program will show the number of items entered. Note that the 0 is not to be treated as an entered number; it is strictly a sentinel value, not data. Make sure all instructions and outputs are clear and meaningful.
Sample session:
Enter a 4-digit unsigned number (0 to stop): 12
Enter a 4-digit unsigned number (0 to stop): 8
Enter a 4-digit unsigned number (0 to stop): 9999
Enter a 4-digit unsigned number (0 to stop): 5
Enter a 4-digit unsigned number (0 to stop): 270
Enter a 4-digit unsigned number (0 to stop): 1
Enter a 4-digit unsigned number (0 to stop): 9876
Enter a 4-digit unsigned number (0 to stop): 0
The number of values entered: 0007
Part B
This will be a modification of lab1a. This program will be named lab1b.cob and will allow for multiple sessions in a run.
The program will ask the user if there is to be another session. An input of “N” to this prompt will stop the program. Any other input value can be treated as if it was a “Y” and will cause the program to initiate a new session identical to that in part A above. Once the session ends, it will then ask the user if there is to be another session.
Sample run:
Another Session (Y/N)? Y
Enter a 4-digit unsigned number (0 to stop): 12
Enter a 4-digit unsigned number (0 to stop): 8
Enter a 4-digit unsigned number (0 to stop): 9999
Enter a 4-digit unsigned number (0 to stop): 5
Enter a 4-digit unsigned number (0 to stop): 270
Enter a 4-digit unsigned number (0 to stop): 1
Enter a 4-digit unsigned number (0 to stop): 9876
Enter a 4-digit unsigned number (0 to stop): 0
The number of values entered: 0007
Another Session (Y/N)? Y
Enter a 4-digit unsigned number (0 to stop): 18
Enter a 4-digit unsigned number (0 to stop): 19
Enter a 4-digit unsigned number (0 to stop): 20
Enter a 4-digit unsigned number (0 to stop): 0
The number of values entered: 0003
Another Session (Y/N)? N
Part C
This will be a modification of lab1a. This program will be named lab1c.cob and will be a batch version of lab1a.cob. The input file will be named “lab1c-in.dat” and will be located in the same directory as the lab1c executable (so do not specify a path for the file in the Select statement). Each record of the file will contain a single 4-digit unsigned number. This program will display to the screen the number of items in the file.
There is no sentinel value within the file; each record is to be treated as data, even if the value on the record is 0. Also, each file represents a single session.
Bonus Points
Bonus points are possible for each of the three parts if the programs can also show how many even numbers were entered in the session for each session. Note that 0 is not to be treated as an entered number; it is strictly a sentinel value, not data.
Explanation / Answer
Lab1b.cbl
IDENTIFICATION DIVISION.
* "This program takes unsigned ints as input and reports the"
* "last value entered before sentinel value in loop fashion."
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FOO-INT PIC 9(4).
01 BAR-INT PIC 9(4).
01 INI-INT PIC 9(4).
01 SESS PIC X(1).
PROCEDURE DIVISION.
000-MAIN.
PERFORM XC-PARAGRAPH
PERFORM A-PARAGRAPH WITH TEST AFTER UNTIL FOO-INT = 0
PERFORM B-PARAGRAPH
PERFORM C-PARAGRAPH.
XC-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
GO TO STOP-PARAGRAPH
ELSE
MOVE FOO-INT TO INI-INT
END-IF.
A-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
DISPLAY " "
ELSE
MOVE FOO-INT TO BAR-INT
END-IF.
B-PARAGRAPH.
DISPLAY " "
DISPLAY "The first number entered: " INI-INT
DISPLAY "The last number entered: " BAR-INT.
C-PARAGRAPH.
DISPLAY "Another Session (Y/N)? "
WITH NO ADVANCING
ACCEPT SESS
IF SESS = "N" OR SESS = "n" THEN
GO TO STOP-PARAGRAPH
ELSE
DISPLAY " "
GO TO 000-MAIN
END-IF.
STOP-PARAGRAPH.
STOP RUN.
Lab1c.cbl
IDENTIFICATION DIVISION.
* "This program takes unsigned ints as input from file and "
* "reports the last value entered before sentinel value."
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE ASSIGN TO "lab1c-in.dat"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INFILE.
01 Int-Record.
05 Int Pic 9(4).
WORKING-STORAGE SECTION.
01 WS-Int-Record.
05 WS-Int Pic 9(4).
01 FOO-INT Pic 9(4).
01 WS-EOF Pic A(1).
PROCEDURE DIVISION.
000-MAIN.
PERFORM INITIALIZE-PARAGRAPH
PERFORM READ-PARAGRAPH WITH TEST BEFORE UNTIL WS-EOF = "Y"
DISPLAY FOO-INT IDENTIFICATION DIVISION.
PROGRAM-ID. LAB1a.
AUTHOR. Wyatt Reid.
* "This program takes unsigned ints as input and reports the"
* "last value entered before sentinel value."
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FOO-INT PIC 9(4).
01 BAR-INT PIC 9(4).
01 INI-INT PIC 9(4).
PROCEDURE DIVISION.
000-MAIN.
PERFORM XC-PARAGRAPH
PERFORM A-PARAGRAPH WITH TEST AFTER UNTIL FOO-INT = 0
PERFORM B-PARAGRAPH
PERFORM STOP-PARAGRAPH.
XC-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
GO TO STOP-PARAGRAPH
ELSE
MOVE FOO-INT TO INI-INT
END-IF.
A-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
EXIT PARAGRAPH
ELSE
MOVE FOO-INT TO BAR-INT
END-IF.
B-PARAGRAPH.
DISPLAY " "
DISPLAY "The first number entered: " INI-INT
DISPLAY "The last number entered: " BAR-INT.
STOP-PARAGRAPH.
STOP RUN.
PERFORM CLOSE-PARAGRAPH
STOP RUN.
INITIALIZE-PARAGRAPH.
OPEN INPUT INFILE.
READ-PARAGRAPH.
READ INFILE NEXT RECORD INTO WS-Int-Record
AT END MOVE "Y" to WS-EOF
NOT AT END MOVE WS-Int TO FOO-INT
END-READ.
CLOSE-PARAGRAPH.
CLOSE INFILE.
Lab1a.cbl
IDENTIFICATION DIVISION.
* "This program takes unsigned ints as input and reports the"
* "last value entered before sentinel value."
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FOO-INT PIC 9(4).
01 BAR-INT PIC 9(4).
01 INI-INT PIC 9(4).
PROCEDURE DIVISION.
000-MAIN.
PERFORM XC-PARAGRAPH
PERFORM A-PARAGRAPH WITH TEST AFTER UNTIL FOO-INT = 0
PERFORM B-PARAGRAPH
PERFORM STOP-PARAGRAPH.
XC-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
GO TO STOP-PARAGRAPH
ELSE
MOVE FOO-INT TO INI-INT
END-IF.
A-PARAGRAPH.
DISPLAY "Enter a 4-digit unsigned number (0 to stop): "
WITH NO ADVANCING
ACCEPT FOO-INT
IF FOO-INT = 0 THEN
EXIT PARAGRAPH
ELSE
MOVE FOO-INT TO BAR-INT
END-IF.
B-PARAGRAPH.
DISPLAY " "
DISPLAY "The first number entered: " INI-INT
DISPLAY "The last number entered: " BAR-INT.
STOP-PARAGRAPH.
STOP RUN.