1 Replies - 8133 Views - Last Post: 19 November 2007 - 04:26 PM Rate Topic: -----

#1 Cobolhelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-August 07

Please Helo with a Cobol program to calculate average

Posted 05 August 2007 - 12:07 PM

The program is suppose to put out a list of scores, with an average at the bottom. The average is all of the scores, minus the lowest score. Then it's suppose to say at the bottom which test number was taken out.

I can't even get the program to compile, and I know there is some missing parts of the code. But I just need some help with setting up the subscripts, searching the table, and outputting the right thing. I am getting this error 5-S User-name SCORE-SUB not unique
5-S User-name SCORE-SUB not unique


Below is the code I have so far, any help would be very appreciated!!
	  IDENTIFICATION DIVISION.
		   PROGRAM-ID.	 SHARUTI.

	   ENVIRONMENT DIVISION.
	   INPUT-OUTPUT SECTION.
	   FILE-CONTROL.
		   SELECT STUDENT-FILE
			   ASSIGN TO 'C:\PCOBWIN\SAMPLES\TEST\SCORES.DAT'
			   ORGANIZATION IS LINE SEQUENTIAL.
		   SELECT PRINT-FILE
			   ASSIGN TO 'C:\PCOBWIN\SAMPLES\TEST\SCORES.DOC'.

	   DATA DIVISION.
	   FILE SECTION.
	   FD STUDENT-FILE
		   RECORD CONTAINS 69 CHARACTERS.
	   01 STUDENT-RECORD.
		   05 STUDENT-NUM				  PIC X(9).
		   05 TEST-SCORES OCCURS 20 TIMES  PIC 9(3).


	   FD PRINT-FILE
		   RECORD CONTAINS 132 CHARACTERS.
	   01 PRINT-LINE   PIC X(132).

	   WORKING-STORAGE SECTION.
	   01 DATA-REMAINS-SWITCH   PIC X(2)  VALUE SPACES.

	   01 SUBSCRIPTS		   COMP.
		   05 SCORE-SUB		PIC S9(4).
		   05 SAVE-SUB		 PIC S9(4).
		   05 SCORES		   PIC X.

	   01 SUBSCRIPTS1		   COMP.
		   05 GROUP1.
			   10 SCORE-SUB1		PIC S9(4).
			   10 SAVE-SUB1		 PIC S9(4).
	   01 FIRST-HEADER.
		   05 FILLER			PIC X(7) VALUE SPACES.
		   05 FILLER			PIC X(26) VALUE
		   'PROFESSOR JONES  REPORT TO'.
		   05 FILLER			PIC X VALUE SPACE.
		   05 STUDENT-NUM	   PIC 999B99B9999.
		   05 FILLER			PIC X(7) VALUE SPACES.
		   05 FILLER			PIC X(4) VALUE 'PAGE'.
		   05 FILLER			PIC X VALUE SPACE.
		   05 PAGE-NO		   PIC X(2).
		   05 FILLER			PIC X(73) VALUE SPACES.

	   01 SECOND-HEADER.
		   05 FILLER		   PIC X(19) VALUE SPACES.
		   05 FILLER		   PIC X(11) VALUE 'TEST SCORES'.
		   05 FILLER		   PIC X(102) VALUE SPACES.

	   01 DETAIL-LINE.
		   05 FILLER		   PIC X(21) VALUE SPACES.
		   05 TEST-NUM		 PIC Z9.
		   05 FILLER		   PIC XBX VALUE SPACES.
		   05 TEST-SCORES	  PIC ZZ9.
		   05 FILLER		   PIC X(103) VALUE SPACES.

	   01 SUBSCRIPTS		   COMP.
		   05 SCORE-SUB		PIC S9(4).
		   05 SAVE-SUB		 PIC S9(4).

	   01 AVERAGE-LINE.
		   05 FILLER		   PIC X(7) VALUE SPACES.
		   05 FILLER		   PIC X(18) VALUE 'TEST SCORE AVERAGE'.
		   05 FILLER		   PIC X VALUE SPACE.
		   05 AVERAGE-SCORE	PIC ZZ9.9.

	   01 FOOTER-LINE.
		   05 FILLER		   PIC X VALUE SPACE.
		   05 FILLER		   PIC X(27)
			   VALUE '*** NOTE: TEST SCORE NUMBER'.
		   05 LOWEST-SCORE	 PIC Z9.
		   05 FILLER		   PIC X.
		   05 FILLER		   PIC X(23)
			   VALUE 'NOT INCLUDED IN AVERAGE'.
		   05 FILLER		   PIC X(78) VALUE SPACES.

	   PROCEDURE DIVISION.
	   100-PROCESS-STUDENT-RECORDS.
		   OPEN INPUT STUDENT-FILE
			   OUTPUT PRINT-FILE.
		   PERFORM UNTIL DATA-REMAINS-SWITCH = 'YES'
			   READ STUDENT-FILE
				   AT END
					   MOVE 'YES' TO DATA-REMAINS-SWITCH
				   NOT AT END
					   PERFORM 200-CREATE-REPORT
			   END-READ
		   END-PERFORM.

		   CLOSE   STUDENT-FILE
				   PRINT-FILE.
		   STOP RUN.

	   200-CREATE-REPORT.
		   PERFORM 210-WRITE-HEADINGS.
		   PERFORM 220-WRITE-SCORES.
		   PERFORM 230-CALCULATE-AVERAGE.
		   PERFORM 240-WRITE-FOOTER.

	   210-WRITE-HEADINGS.
		   MOVE FIRST-HEADER TO PRINT-LINE.
		   WRITE PRINT-LINE.
		   MOVE SECOND-HEADER TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 2 LINES.

	   220-WRITE-SCORES.
		   PERFORM 225-INCREMENT-AVERAGE
			   VARYING SCORE-SUB1 FROM 1 BY 1
				   UNTIL SCORE-SUB1 > 20.
		   MOVE DETAIL-LINE TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 1 LINE.

	   225-INCREMENT-AVERAGE.
		   ADD SCORES (SCORE-SUB) TO SCORES.
		   MOVE SCORES TO PRINT-LINE.
		   WRITE PRINT-LINE
		   AFTER ADVANCING 1 LINE.

	   230-CALCULATE-AVERAGE.
		   COMPUTE AVERAGE-SCORE =
			   SCORES (SCORE-SUB) +
			   AVERAGE-SCORE / 19
		   END-COMPUTE.

	   240-WRITE-FOOTER.
		   MOVE FOOTER-LINE TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 1 LINE.
IDENTIFICATION DIVISION.
		   PROGRAM-ID.	 SHARUTI.

	   ENVIRONMENT DIVISION.
	   INPUT-OUTPUT SECTION.
	   FILE-CONTROL.
		   SELECT STUDENT-FILE
			   ASSIGN TO 'C:\PCOBWIN\SAMPLES\TEST\SCORES.DAT'
			   ORGANIZATION IS LINE SEQUENTIAL.
		   SELECT PRINT-FILE
			   ASSIGN TO 'C:\PCOBWIN\SAMPLES\TEST\SCORES.DOC'.

	   DATA DIVISION.
	   FILE SECTION.
	   FD STUDENT-FILE
		   RECORD CONTAINS 69 CHARACTERS.
	   01 STUDENT-RECORD.
		   05 STUDENT-NUM				  PIC X(9).
		   05 TEST-SCORES OCCURS 20 TIMES  PIC 9(3).


	   FD PRINT-FILE
		   RECORD CONTAINS 132 CHARACTERS.
	   01 PRINT-LINE   PIC X(132).

	   WORKING-STORAGE SECTION.
	   01 DATA-REMAINS-SWITCH   PIC X(2)  VALUE SPACES.

	   01 SUBSCRIPTS		   COMP.
		   05 SCORE-SUB		PIC S9(4).
		   05 SAVE-SUB		 PIC S9(4).
		   05 SCORES		   PIC X.

	   01 SUBSCRIPTS1		   COMP.
		   05 GROUP1.
			   10 SCORE-SUB1		PIC S9(4).
			   10 SAVE-SUB1		 PIC S9(4).
	   01 FIRST-HEADER.
		   05 FILLER			PIC X(7) VALUE SPACES.
		   05 FILLER			PIC X(26) VALUE
		   'PROFESSOR JONES  REPORT TO'.
		   05 FILLER			PIC X VALUE SPACE.
		   05 STUDENT-NUM	   PIC 999B99B9999.
		   05 FILLER			PIC X(7) VALUE SPACES.
		   05 FILLER			PIC X(4) VALUE 'PAGE'.
		   05 FILLER			PIC X VALUE SPACE.
		   05 PAGE-NO		   PIC X(2).
		   05 FILLER			PIC X(73) VALUE SPACES.

	   01 SECOND-HEADER.
		   05 FILLER		   PIC X(19) VALUE SPACES.
		   05 FILLER		   PIC X(11) VALUE 'TEST SCORES'.
		   05 FILLER		   PIC X(102) VALUE SPACES.

	   01 DETAIL-LINE.
		   05 FILLER		   PIC X(21) VALUE SPACES.
		   05 TEST-NUM		 PIC Z9.
		   05 FILLER		   PIC XBX VALUE SPACES.
		   05 TEST-SCORES	  PIC ZZ9.
		   05 FILLER		   PIC X(103) VALUE SPACES.

	   01 SUBSCRIPTS		   COMP.
		   05 SCORE-SUB		PIC S9(4).
		   05 SAVE-SUB		 PIC S9(4).

	   01 AVERAGE-LINE.
		   05 FILLER		   PIC X(7) VALUE SPACES.
		   05 FILLER		   PIC X(18) VALUE 'TEST SCORE AVERAGE'.
		   05 FILLER		   PIC X VALUE SPACE.
		   05 AVERAGE-SCORE	PIC ZZ9.9.

	   01 FOOTER-LINE.
		   05 FILLER		   PIC X VALUE SPACE.
		   05 FILLER		   PIC X(27)
			   VALUE '*** NOTE: TEST SCORE NUMBER'.
		   05 LOWEST-SCORE	 PIC Z9.
		   05 FILLER		   PIC X.
		   05 FILLER		   PIC X(23)
			   VALUE 'NOT INCLUDED IN AVERAGE'.
		   05 FILLER		   PIC X(78) VALUE SPACES.

	   PROCEDURE DIVISION.
	   100-PROCESS-STUDENT-RECORDS.
		   OPEN INPUT STUDENT-FILE
			   OUTPUT PRINT-FILE.
		   PERFORM UNTIL DATA-REMAINS-SWITCH = 'YES'
			   READ STUDENT-FILE
				   AT END
					   MOVE 'YES' TO DATA-REMAINS-SWITCH
				   NOT AT END
					   PERFORM 200-CREATE-REPORT
			   END-READ
		   END-PERFORM.

		   CLOSE   STUDENT-FILE
				   PRINT-FILE.
		   STOP RUN.

	   200-CREATE-REPORT.
		   PERFORM 210-WRITE-HEADINGS.
		   PERFORM 220-WRITE-SCORES.
		   PERFORM 230-CALCULATE-AVERAGE.
		   PERFORM 240-WRITE-FOOTER.

	   210-WRITE-HEADINGS.
		   MOVE FIRST-HEADER TO PRINT-LINE.
		   WRITE PRINT-LINE.
		   MOVE SECOND-HEADER TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 2 LINES.

	   220-WRITE-SCORES.
		   PERFORM 225-INCREMENT-AVERAGE
			   VARYING SCORE-SUB1 FROM 1 BY 1
				   UNTIL SCORE-SUB1 > 20.
		   MOVE DETAIL-LINE TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 1 LINE.

	   225-INCREMENT-AVERAGE.
		   ADD SCORES (SCORE-SUB) TO SCORES.
		   MOVE SCORES TO PRINT-LINE.
		   WRITE PRINT-LINE
		   AFTER ADVANCING 1 LINE.

	   230-CALCULATE-AVERAGE.
		   COMPUTE AVERAGE-SCORE =
			   SCORES (SCORE-SUB) +
			   AVERAGE-SCORE / 19
		   END-COMPUTE.

	   240-WRITE-FOOTER.
		   MOVE FOOTER-LINE TO PRINT-LINE.
		   WRITE PRINT-LINE
			   AFTER ADVANCING 1 LINE.



Is This A Good Question/Topic? 0
  • +

Replies To: Please Helo with a Cobol program to calculate average

#2 67Rambler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 07

Re: Please Helo with a Cobol program to calculate average

Posted 19 November 2007 - 04:26 PM

Hello, Sharuti.

I don't have a compiler handy, but from what I've seen, everything can be done in one program using tables. If you have the same paragraph names in the main and nested program, the compiler may not be able to resolve the addresses; just rename the paragraphs, prefixing them with M in the main and S in the sub-program.

From what I can see, you are expecting 20 scores (with a divisor of 19 indicating that the lowest score has been dropped). The easiest way is with a sort using and giving, dumping the scores into a table from which you will list the top 19. A slightly more onerous way is with two swap tables, similar to a bubble sort.

No matter what, there should be a single field that holds all 9s. When you read a score, you check against that field. In all probability, the first score will be less than the initial field value. The score is then moved into the field. Without writing th entire program, this would appear in the working-storage section:
77 SCORE-FLAG PIC X VALUE "N".
77 LOW-SCORE PIC S9(7)v9(6) COMP-3 VALUE 9999999.999999.
77 LOW-SCORE-INDX PIC S9(4) COMP VALUE 1.
77 SCORE-INDX PIC s9(4) COMP.
77 SCORE-INDX-LIMIT PIC S9(3) COMP-3 VALUE 20.
01 SCORE-TABLE.
05 ST-SCORE OCCURS 20 TIMES
PIC S9(7)v9(6) COMP-3.

Start something like this:
PROCEDURE DIVISION.
M1-HOUSEKEEPING.
MOVE "N" TO SCORE-FLAG.
MOVE ZEROES TO SCORE-INDX.
PERFORM UNTIL SCORE-INDX IS GREATER THAN SCORE-INDX-LIMIT
MOVE ZEROES TO ST-SCORE (SCORE-INDX)
END-PERFORM.
OPEN INPUT SCORE-FILE.
MOVE ZEROES TO SCORE-INDX.
PERFORM M2-BUILD-TABLE UNTIL SCORE-FLAG IS EQUAL TO "Y".
CLOSE SCORE-FILE.
OPEN OUTPUT REPORT-FILE.
MOVE "N" TO SCORE-FLAG.
PERFORM N3-WRITE-REPORT UNTIL SCORE-FLAG IS EQUAL TO "Y".
CLOSE REPORT-FILE.
GOBACK.

M2-BUILD-TABLE.
READ SCORE-FILE
AT END
MOVE "Y" TO SCORE-FLAG.
IF SCORE-FLAG IS EQUAL TO "N"
IF SCORE-INDX IS NOT GREATER THAN SCORE-INDX-LIMIT
IF SCORE-FILE-SCORE IS LESS THAN LOW-SCORE
MOVE SCORE-FILE-SCORE TO LOW-SCORE
ADD 1 TO SCORE-INDX
MOVE SCORE-INDX TO LOW-SCORE-INDX
MOVE SCORE-FILE-SCORE TO ST-SCORE (SCORE-INDX)
ELSE
MOVE "Y" TO SCORE-FLAG.
M2-EXIT.

At this point, all the scores are in the table and the lowest score has been selected. In addition, all 20 scores have been processed.

If you want to print the scores as they were entered, (for example, 80, 92, 87, 72, 65, 98), the lowest score in that list has an index of 5. Just bypass the fifth entry and print the scores. When printing the lowest score, use the LOW-SCORE-INDX to print the lowest entry.

The code above is the way I coded for 25 years; I don't use what's not necessary. The COBOL AND is the same thing as an IF within an IF. The COBOL OR is the same thing as IF-ELSE. If I don't have an else statement, I don't use ELSE. I also use flags (bits) that will be yes-no or on-off; that's the purpose of the SCORE-FLAG above.

See how this works. I never used nested programs, preferring separate sub-programs that could be used by other routines. One bad thing about writing in this is that there are no indents, so you'll have to reformat the code to match what you are doing. Also, the entire working storage section dealing with the front end can be coded in one level-01.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1