COBOL Program to implement
Addition of Two Matrices.
PROGRAM:
IDENTIFICATION DIVISION.
PROGRAM-ID MAT-ADD.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MAT1.
02 MR1 OCCURS 5 TIMES.
03 MC1 PIC 9(2) OCCURS 5 TIMES.
01 MAT2.
02 MR2 OCCURS 5 TIMES.
03 MC2 PIC 9(2) OCCURS 5 TIMES.
01 MAT3.
02 MR3 OCCURS 5 TIMES.
03 MC3 PIC ZZ9 OCCURS 5 TIMES.
77 ROW1 PIC 9.
77 ROW2 PIC 9.
77 COL1 PIC 9.
77 COL2 PIC 9.
77 I PIC 9.
77 J PIC 9.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "MATRIX ADDITION".
DISPLAY " ".
DISPLAY "ENTER THE NUMBER OF
ROW AND COLUMNS OF 1 MATRIX".
ACCEPT ROW1.
ACCEPT COL1.
DISPLAY "ENTER THE
NUMBER OF ROWS AND COLUMNS OF 2ND MATRIX".
ACCEPT ROW2.
ACCEPT COL2.
IF(ROW1 IS NOT EQUAL TO ROW2) OR
(COL1 NOT EQUAL TO COL2)
DISPLAY "MATRIX ADDITION IS NOT
POSSIBLE"
ELSE
PERFORM COMPU-PARA
STOP RUN.
COMPU-PARA.
DISPLAY "ENTER THE ELEMENTS OF
MATRIX1".
PERFORM ACCPARA1 VARYING I FROM 1 BY
1 UNTIL I > ROW1
AFTER J FROM 1 BY 1 UNTIL J>COL1.
DISPLAY "ENTER THE ELEMENTS OF MATRIX2".
PERFORM ACCPARA2 VARYING I FROM 1 BY
1 UNTIL I > ROW2
AFTER J FROM 1 BY 1 UNTIL J>COL2.
PERFORM ADDMAT-PARA VARYING I FROM 1
BY 1 UNTIL I > ROW1
AFTER J FROM 1 BY 1 UNTIL J>COL1.
DISPLAY ( 1 1 ) ERASE.
MOVE 3 TO LIN.
MOVE 10 TO COL.
DISPLAY " ".
DISPLAY "THE FIRST MATRIX
IS".
DISPLAY " ".
PERFORM DISP1-PARA VARYING I FROM 1
BY 1 UNTIL I > ROW1
AFTER J FROM 1 BY 1 UNTIL J > COL1.
DISPLAY " ".
MOVE 10 TO LIN.
MOVE 10 TO COL.
DISPLAY " ".
DISPLAY "THE SECOND MATRIX
IS".
DISPLAY " ".
PERFORM DISP2-PARA VARYING I FROM 1
BY 1 UNTIL I > ROW2
AFTER J FROM 1 BY 1 UNTIL J >
COL2
DISPLAY " ".
MOVE 17 TO LIN.
MOVE 10 TO COL.
DISPLAY " ".
DISPLAY "THE RESULTANT MATRIX
IS".
DISPLAY " ".
PERFORM DISP-PARA VARYING I FROM 1
BY 1 UNTIL I > ROW1
AFTER J FROM 1 BY 1 UNTIL J >
COL1.
DISPLAY " ".
ACCPARA1.
ACCEPT MC1 (I J).
ACCPARA2.
ACCEPT MC2 (I J).
ADDMAT-PARA.
COMPUTE MC3 (I J) = MC1 (J I) + MC2
(I J).
DISP-PARA.
DISPLAY (LIN, COL)
MC3(I J).
ADD 5 TO COL.
IF J IS EQUAL TO COL1
ADD 1 TO LIN MOVE 10 TO COL.
DISP1-PARA.
DISPLAY (LIN, COL)
MC1(I J).
ADD 5 TO COL.
IF J IS EQUAL TO COL
ADD 1 TO LIN MOVE 10 TO COL.
DISP2-PARA.
DISPLAY (LIN, COL)
MC2(I J).
ADD 5 TO COL.
IF J IS EQUAL TO COL1
ADD 1 TO LIN MOVE 10 TO COL.
No comments:
Post a Comment