Array Pseudocode
Array Pseudocode
أ
Programming Fundamental Lec11 Sem1
Two-Dimensional Array
Contents of Lecture:
Introduction
Declaration and Initialization
Processing 2D Arrays
Example
Homework number (8) delivered next week
Introduction
While one-dimensional arrays allow data to be placed in an array one row at a time,
two-dimensional arrays are capable of storing data in both rows and columns.
Because of the capability of storing data in rows and columns, it is obvious that two-
dimensional arrays can provide more flexibility than one-dimensional arrays.
To declare an array of five rows and three columns, the following code could be used:
Declare arr[5][3].
Page 1 of 6
علوم حاسوب ابتسام أبكر.أ
Programming Fundamental Lec11 Sem1
Or declare a two-dimensional array by initial values; we can be accomplished
using the following format:
int arr[5][3] =
{
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 },
{ 9, 0, 1 },
{ 2, 3, 4 } 0 1 2
}
0 0 1 2
1 3 4 5
2 6 7 8
3 9 0 1
4 2 3 4
The above code initializes:
arr[0][0] = 0
arr[0][1] = 1
arr[1][0] = 3
arr[1][1] = 4
arr[4][0] = 2
,…
Processing 2D Arrays
Because 2D arrays must be filled by row and column, processing a 2D array can be
done using nested for loops.
For instance, to fill an array declared numArr[10][10] with user input, the following
nested loop scheme could be used:
For row = 0 to 9
For col = 0 to 9
Prompt user for numArr[row][col]
Get numArr[row][col]
col col +1
EndLoop
row row +1
EndLoop
Page 2 of 6
علوم حاسوب ابتسام أبكر.أ
Programming Fundamental Lec11 Sem1
To display the contents of the above filled array ten values per line, the following
code could be used:
For row = 0 to 9
For col = 0 to 9
Display numArr[row][col]
col col +1
EndLoop
row row +1
EndLoop
Example:
Write pseudo code and draw flowchart to input and output number in two-dimensional
array of 3 rows and four column.
1. Begin
2. Declare arrNum[3][4]
3. For i = 0 to 3
For k = 0 to 4
Prompt user to enter arrNum[i][k]
Get arrNum[i][k]
k = k +1
EndLoop
i=i+1
EndLoop
4. For i = 0 to 2
For k = 0 to 3
Display arrNum[i][k]
k = k +1
EndLoop
i=i+1
EndLoop
5. End
Page 3 of 6
علوم حاسوب ابتسام أبكر.أ
Programming Fundamental Lec11 Sem1
Start
i=0
If NO
( i <= 2)
YES
k=0
NO If
( k <= 3)
YES
Get arrNum[i][k]
i=i+1
k = k +1
Page 4 of 6
علوم حاسوب ابتسام أبكر.أ
Programming Fundamental Lec11 Sem1
i=0
If NO
( i <= 2)
YES
k=0
NO If
( k <= 3)
YES
Display arrNum[i][k]
k = k +1
i=i+1
End
Page 5 of 6
علوم حاسوب ابتسام أبكر.أ
Programming Fundamental Lec11 Sem1
2) Write a pseudo code and draw a flowchart that displays the maximum and minimal
value in two-dimensional array that have size 6×4.
3) write a pseudo code and draw a flowchart to display the summation of each row in
two-dimension array of three rows and three columns, for example if you input
values like the figure below
2 8 7
5 5 1
4 3 9
The output should like this:
17
11
16
Page 6 of 6