0% found this document useful (0 votes)
9 views6 pages

Pseudocode for 2D Array Operations

The document provides pseudocode for three modules: SetRow(), SearchInRow(), and GetCentreCol() which manipulate a 2D array representing a screen. It also includes pseudocode for sorting a 2D array column-wise using Bubble Sort and an alternative smart sorting method. The algorithms focus on setting values in a row, searching for values, and sorting based on specific criteria.

Uploaded by

Haris
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Pseudocode for 2D Array Operations

The document provides pseudocode for three modules: SetRow(), SearchInRow(), and GetCentreCol() which manipulate a 2D array representing a screen. It also includes pseudocode for sorting a 2D array column-wise using Bubble Sort and an alternative smart sorting method. The algorithms focus on setting values in a row, searching for values, and sorting based on specific criteria.

Uploaded by

Haris
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1

Alternative
Q2
(a) Write pseudo code to implement the module SetRow().

PROCEDURE SetRow(Row, SkipNum, SetNum : INTEGER)


DECLARE Col : INTEGER

// array is 1280 x 800

FOR Col ← SkipNum + 1 TO SkipNum + SetNum


Screen[Row, Col] ← 1
NEXT Index
ENDPROCEDURE
(b) Write pseudocode to implement the module SearchInRow()
FUNCTION SearchInRow(ThisRow, StartCol : INTEGER) RETURNS INTEGER
DECLARE ThisCol, Step : INTEGER
DECLARE Found: BOOLEAN
// array is 1280 x 800
Found ← FALSE
ThisCol ← StartCol
// first decide which way to search
IF StartCol = 1 THEN
Step ← 1
EndCol ← 1281
ELSE
Step ← -1
EndCol ← 0
ENDIF

WHILE ThisCol <> EndCol AND Found = FALSE


IF Screen[ThisRow, ThisCol] <> 1 THEN
ThisCol ← ThisCol + Step
ELSE
Found ← TRUE
ENDIF
ENDWHILE
IF Found = FALSE THEN
ThisCol ← -1
ENDIF
RETURN ThisCol
ENDFUNCTION
(c) Write pseudocode to implement the module GetCentreCol().

FUNCTION GetCentreCol(ThisRow : INTEGER) RETURNS INTEGER


DECLARE StartCol, EndCol, CentreCol : INTEGER
StartCol ← SearchInRow(ThisRow, 1)
IF StartCol = -1 THEN
CentreCol ← StartCol
ELSE
EndCol ← SearchInRow(ThisRow, 1280)
CentreCol ← INT((StartCol + EndCol)/2)
ENDIF
RETURN CentreCol
ENDFUNCTION
Q

Sorting 2D Arrays Colum wise

PROCEDURE BubbleSort(arr[][] OF )
FOR i  1T0 1000
FOR j  1T0 2
IF (arr[j , 2] > arr[j + 1, 2]):
#Swap the jobs if they are out of order based on priority
Temp1  arr[j , 1]
arr[j ,1]  arr[j + 1, 1]
arr[j + 1, 1]  temp1

temp2  arr[j ,2]


arr[j , 2]  arr[j + 1, 2]
arr[j + 1, 2]  temp2
END IF
NEXT
NEXT
END PROCEDURE

Alternative Smart Code


Solution

PROCEDURE Sort()
DECLARE Temp : INTEGER
DECLARE Flag : BOOLEAN
DECLARE Boundary, Row, Col : INTEGER
Boundary ← 999
REPEAT
Flag ← TRUE
FOR Row ← 1 TO Boundary
IF Result[Row, 2] > Result[Row + 1, 2] THEN
FOR Col ← 1 TO 2
Temp ← Result [Row, Col]
Result [Row, Col] ← Result [Row + 1, Col]
Result [Row + 1, Col] ← Temp
NEXT Col
Flag ← FALSE
ENDIF
NEXT J
Boundary ← Boundary - 1
UNTIL Flag = TRUE
ENDPROCEDURE

You might also like