0% found this document useful (0 votes)
16 views30 pages

2D arrays_problems_2

The document provides an overview of using 2D arrays in pseudocode, including tasks such as computing the largest and smallest elements in rows and columns, as well as calculating sums and transposing arrays. It includes pseudocode examples for various tasks related to 2D arrays, specifically for a 3x4 array. Additionally, it references resources for further study on the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views30 pages

2D arrays_problems_2

The document provides an overview of using 2D arrays in pseudocode, including tasks such as computing the largest and smallest elements in rows and columns, as well as calculating sums and transposing arrays. It includes pseudocode examples for various tasks related to 2D arrays, specifically for a 3x4 array. Additionally, it references resources for further study on the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

2D arrays

LO: To use 2D arrays in pseudocodes.


Keywords: row index, column index
CCL: Maths
Starter
Devise the row and column index
of the below array and identify
each element.
Assign 20 to DataArray[2,1]
Assign 15 to DataArray[3,3]
Size of the array?
Starter-Answer
Devise the row and column index
of the below array and identify
each element.
Size is 3 x 4
1,1 1,2 1,3 1,4
2,1 20 2,2 3,2 4,2
3,1 3,2 3,3 15 4,3
Scenario-Think, Share, Pair
• Consider the rows of the array be the different subject marks
and the columns of the array be different students.
• We need to compute:
• What does Largest element in the rows indicate?
• What does smallest element in the rows indicate?
• What does the largest element in the column indicate?
• What does the smallest element in the column indicate?
Task
• Given a class of 30 students, compute the highest and lowest mark
obtained in Computer Science. Assume is already filled with the
marks of students.
Sol
• DECLARE MarkArray: ARRAY[1:30] OF INTEGER
• DECLARE Min, Max, Index: INTEGER
• Min MarkArray[1]
• Max  MarkArray[1]
• FOR Index  2 TO 30
• IF MarkArray[Index] < Min
• THEN
• Min  MarkArray[Index]
• ELSE
• IF MarkArray[Index] > Max
• THEN
• Max  MarkArray[Index]
• ENDIF
• ENDIF
• NEXT Index
• OUTPUT “The maximum mark is”, Max, “The minimum mark is”, Min
Sol
Task -1
• Compute the total(sum) of all the elements of the rows of a
2D array and store them in another 1D array.
• Consider a 3x4 array.
Sol-1
• DECLARE M : ARRAY[1:3,1:4] OF INTEGER
• DECLARE C : ARRAY[1:3] OF INTEGER
• FOR row  1 TO 3
• Total  0
• FOR col  1 TO 4
• Total  Total + M[row,col]
• NEXT col
• C[row]  Total
• OUTPUT “The row sum is”, Total
• NEXT row
Task-2
• Compute the total(sum) of all the elements in the columns of
a 2D array and store them in another 1D array.
• Consider a 3x4 array. Assume array is already filled.
Sol-2
• DECLARE TotalArray : ARRAY[1:3,1:4] OF INTEGER
• DECLARE ColTotal : ARRAY[1:3] OF INTEGER
• FOR ColIndex  1 TO 4
• Total  0
• FOR RowIndex  1 TO 3
• Total  Total + TotalArray[RowIndex,ColIndex]
• NEXT RowIndex
• ColTotal[ColIndex]  Total
• OUTPUT “The col sum is”, ColTotal[ColIndex]
• NEXT ColIndex
Task 3
• Compute the largest element in each row of a 2D array(3 x 4). Assume
array is already filled.
Sol-3
• DECLARE DataArray : ARRAY[1:3,1:4] OF INTEGER
• DECLARE MaxArray : ARRAY[1:3] OF INTEGER
• FOR RowIndex  1 TO 3
• Large  DataArray[RowIndex,1]
• FOR ColIndex  1 TO 4
• IF DataArray[RowIndex,ColIndex] > Large
• THEN
• Large  DataArray[RowIndex,ColIndex]
• ENDIF
• NEXT ColIndex
• N[RowIndex]  Large
• OUTPUT “ The largest elements are”, N[RowIndex]
• NEXT RowIndex
Task 4(HW)
• Compute the smallest element in each row of a 2D array.
Sol-4
• DECLARE M : ARRAY[1:3,1:4] OF INTEGER
• DECLARE N : ARRAY[1:3] OF INTEGER
• FOR row  1 TO 3
• S  M[row,1]
• FOR col  1 TO 4
• IF M[row,col] < S
• THEN
• S  M[row,col]
• ENDIF
• NEXT col
• N[row]  S
• NEXT row
Task 5
• Compute the smallest element in each column of a 2D array. Assume
array(3 x 4) is already filled.
Sol-5(TO find smallest element in each column
of a 2D array)
• DECLARE DataArray : ARRAY[1:3,1:4] OF INTEGER
• DECLARE MinArray : ARRAY[1:3] OF INTEGER
• FOR ColIndex  1 TO 4
• Min  DataArray[1,ColIndex]
• FOR RowIndex  1 TO 3
• IF DataArray[RowIndex,ColIndex] < Min
• THEN
• Min  DataArray[RowIndex,ColIndex]
• ENDIF
• NEXT RowIndex
• MinArray[ColIndex]  Min
• NEXT ColIndex
Task 6(HW)
• Compute the largest element in each column of a 2D array. Assume
array(3 x 4) is already filled.
Sol-6
• DECLARE DataArray : ARRAY[1:3,1:4] OF INTEGER
• FOR col  1 TO 4
• L  M[1,col]
• FOR row  1 TO 3
• IF M[row,col] > L
• THEN
• L  M[row,col]
• ENDIF
• NEXT row
• N[col]  L
• NEXT col
Task 7:
• Develop a pseudocode to find the transpose of a 2D array having 3
rows and 4 columns.
Sol-7(to find the transpose of a 2D array having
3 rows and 4 columns.)
• DECLARE Data1Array : ARRAY[1:3,1:4] OF INTEGER
• DECLARE Data2Array : ARRAY[1:4,1:3] OF INTEGER
• FOR RowIndex  1 TO 3
• FOR ColIndex  1 TO 4
• Data2Array[ColIndex,RowIndex] 
Data1Array[RowIndex,ColIndex]
• NEXT ColIndex
• NEXT RowIndex
• OUTPUT”The transpose matrix is”,
Data2Array[ColIndex,RowIndex]
Task
Sol
• This pseudocode algorithm is intended to allow data for up to 50
people to be entered and stored in a two-dimensional (2D) array. The
data is their last name, first name and the city in which they live.
HW
• Calculate the average of the elements in a row of a 2D array.
References
• https://www.savemyexams.com/igcse/computer-science/cie/23/topic
-questions/8-programming/8-2-arrays/-/-/medium/
• https://www.youtube.com/watch?v=5nOyfjXF_Ko

You might also like