Topic 5-2D array
1. Construct an algorithm to print all the even number from a 2D array MAT of size
6*5.
Loop I from 0 to 5
Loop J from 0 to 4
if MAT[I][J] mod 2=0 then
ouptut MAT[I][J]
end if
end Loop
end Loop
2. Construct an algorithm to find the average value from a 2D array MAT of size 6*5.
SUM=0
Loop I from 0 to 5
Loop J from 0 to 4
SUM=SUM+ MAT[I][J]
end if
end Loop
end Loop
AVG=SUM/30
output AVG
3.Construct an algorithm to find the number of values more than the average value from a
2D array MAT of size 6*5. Les assume average is stored in variable AVG.
COUNT=0
Loop I from 0 to 5
Loop J from 0 to 4
if MAT[I][J]>AVG then
COUNT=COUNT+1
end if
end Loop
end Loop
output "VALUEs MORE THAN AVERAGE ARE",COUNT
Construct an algorithm to find the sum of Construct an algorithm to find the sum of
left diagonal element. right diagonal element.
SUM=0 SUM=0
N=5 N=5
Loop I from 0 to N-1 Loop I from 0 to N-1
SUM=SUM+MAT[I][I]
end Loop SUM=SUM+MAT[I][N-1-I]
output SUM end Loop
output SUM
Construct a subprogram that accept the 2d array mat of size N and return true if all the
element on right and left diagonal are zero otherwise return false.
Subprogram valid(MAT, N) // Check right (anti) diagonal element
FLAG = True if MAT[I][N - 1 - I] ≠ 0 then
Loop I from 0 to N - 1 FLAG = False
// Check left (main) diagonal element Break
if MAT[I][I] ≠ 0 then end if
FLAG = False End Loop
Break Return FLAG
end if End Subprogram
Construct an algorithm to add all the elements of a matrix of size N, present above left
diagonal.
SUM=0 OR
Loop I from 0 to N-1 SUM=0
Loop J from 0 to N-1 Loop I from 0 to N-1
if J>I then Loop J from I+1 to N-1
SUM=SUM+ MAT[I][J] SUM=SUM+ MAT[I][J]
end if
end if end Loop
end Loop end Loop
end Loop output SUM
output SUM
Construct an algorithm to find the sum of Construct a subprogram sumleftdiagonal
left diagonal element in a 2D array MAT that accept a matrix and its size and return
of size N*N. the sum of left diagonal element.
SUM=0 subprogram sumleftdiagonal(MAT,N)
Loop I from 0 to N-1 SUM=0
SUM=SUM+MAT[I][I] Loop I from 0 to N-1
end Loop SUM=SUM+MAT[I][I]
output SUM end Loop
return SUM
end subprogram
Construct an algorithm to find the sum of Construct an algorithm for subprogram
right diagonal element in a 2D array MAT sumrightdiagonal that accept a matrix and
of size N*N. its size and return the sum of right
diagonal element.
SUM=0 subprogram sumrightdiagonal(MAT,N)
Loop I from 0 to N-1 SUM=0
SUM=SUM+MAT[I][N-1-I] Loop I from 0 to N-1
end Loop SUM=SUM+MAT[I][N-1-I]
output SUM end Loop
return SUM
end subprogram
Construct an algorithm to print all the elements below left diagonal
Loop I from 0 to N-1 OR
Loop J from 0 to N-1 Loop I from 0 to N-1
if j<i then Loop J from 0 to I-1
output MAT[I][J] output MAT[I][J]
end if end if
end Loop
end Loop end Loop
end Loop
Construct an algorithm to check whether the 2d array is valid or not. if all the element
above the left diagonal are non-zero and below the left diagonal are zero then output it is
valid array otherwise output it is invalid.
FLAG = 0 OR
Loop I from 0 to N-1 FLAG = 0
Loop J from 0 to N-1 Loop I from 0 to N-1
if j < i then // Below the diagonal: //Loop checking below diagonal
should be 0 Loop J from 0 to I-1
if MAT[I][J] ≠ 0 then if MAT[I][J] ≠ 0 then
FLAG = 1 FLAG = 1
break break
end if end if
end Loop
else if j > i then // Above the diagonal: if FLAG=0 then
should be non-zero //Loop checking above diagonal
if MAT[I][J] = 0 then Loop J from I+1 to N-1
FLAG = 1 if MAT[I][J] = 0 then
break FLAG = 1
end if break
End Loop end if
End Loop
if FLAG = 1 then
break if FLAG = 1 then
End Loop break
End Loop
if FLAG = 0 then
Output "Array is valid" if FLAG = 0 then
else Output "Array is valid"
Output "Array is not valid" else
Output "Array is not valid"
List of 2 D array questions from past year paper
2.
3.
4.