-------| PROGRAM P-CODE |--------------- -------| IN-LINE COMMENTS |-------
PROGRAM Proj5: ## CGS 2462 - Program 5: Sorting
DECLARE a,b,c : array [1..10] of integer ## Declare global arrays
i : integer ## Declare array index
END-DECLARE
BEGIN-PROGRAM-BODY
WRITELN('>>> Enter 10 numbers [0..100] on one line, separated by spaces')
FOR i = 1 TO 10 DO ## Input ten numbers from keybd
READ(a[i])
c[i] := a[i] ## Make copy of a for output
ENDFOR
BubbleSort(10) ## Sort numbers in ascending order
FOR i = 1 TO 10 DO ## Output numbers and sorted
WRITELN(' INPUT OUTPUT') ## numbers to screen
WRITELN('a[',i,'] = ', c[i], ' b[',i,'] = ', b[i])
ENDFOR
END-PROGRAM-BODY
PROCEDURE BubbleSort(N): ## CGS 2462 - Bubble Sorting Proc.
DECLARE N,minimum,maximum : integer ## Declare local bounds
i,j,imin,imax,flag : integer ## Declare loop indices
END-DECLARE
BEGIN-PROCEDURE-BODY
minimum := 9999 ## Set bounds and flag values
flag := minimum
FOR j = 1 TO N DO: ## For each of N input data
FOR i = 1 TO N DO: ## Search for the array minimum
IF ( a[i] < minimum ) THEN
minimum := a[i]
imin := i ## Save the location where
ENDIF ## minimum was found
ENDFOR
b[j] := a[imin] ## Put the found min. in output
a[imin] := flag ## Reset the input minimum
ENDFOR
END-PROCEDURE-BODY
Step 2. Be careful with BEGIN and END
statements! Remember that they delimit a functional block. If
there are multiple statements within a FOR or IF
statement, then that block of statements must be delimited by
BEGIN and END statements. In the preceding
p-code example, I use BEGIN-PROGRAM-BODY and
END-PROGRAM-BODY for this purpose, and symmetrically for
the procedure body.
Step 3. Based on your experience with errors in Project 4, and our discussion in class, check to see if there are any errors in the preceding program statements. Since we discussed the code in detail in class, I have removed some of the mistakes and revised the array declarations, to make the project a little easier. However, you still need to check for missing semicolons, or wrong variable names, etc.
Run the program with test numbers and see if the output is correct. If the output is sorted incorrectly, why does this occur? Hint: Trace the p-code example, above, to verify its functionality.
Step 4. Run the program with the following test data:
20 43 68 57 44 25 22 20 34 44Your output should have the following form:
INPUT OUTPUT a[1] = 20 b[1] = 20 a[2] = 43 b[2] = 20 a[3] = 68 b[3] = 22 a[4] = 57 b[4] = 25 a[5] = 44 b[5] = 34 a[6] = 25 b[6] = 43 a[7] = 22 b[7] = 44 a[8] = 20 b[8] = 44 a[9] = 34 b[9] = 57 a[10] = 44 b[10] = 68Now, run the program with ten integers of your choosing, ranging from 0 to 100.
Step 5. Document the program produced in Step 1 by
inserting in-line comments using braces ( {
Hardcopy. The completed program (Proj5 together with the two output lists) should be printed on a CIRCA 24-pin printer, or a good-quality printer you may have at home. Laser printer output is acceptable, if you have access to a laser printer. Otherwise, a 24-pin dot matrix printer will do. Output should be dark, so I can read the print. Staple your two pages (one program, one output page) together and write your NAME, SSN, CGS2462, and today's DATE at the TOP RIGHT-HAND CORNER of the paper, in BIG BLOCK LETTERS (so I will not mistake it for someone else's). Be sure to print clearly, so I will know whose program it is.
Items Due. Give the instructor your completed project hardcopy in class on Thursday 13 November 97. Projects turned in a day late will be penalized -10%; two days late: -20%. Projects turned in after the Friday following the due date will not be accepted unless accompanied by a documented excuse (note from your physician or advisor).