Arrays 1
Arrays 1
Accessing an element for a 1D array one index is required and for 2D array two indices are required.
Declaring Arrays:
When an array is declared, the computer sets aside memory to store the values of the elements of
the array.
Declaring an array means telling the computer what is the data type of the array and how many
elements will be stored in it.
Pseudocode to declare 1D array:
o DECLARE <Identifier name> : ARRAY[Lowerbound : Upperbound] OF data type
o DECLARE Weight : ARRAY [1:5] OF INTEGER
The above pseudoccode statement declares an array with 5 elements, all of which must be integers
Page 1 of 6
For example the following illustration shows an array named Weight is having 5 elements in it.
Weight 50 85 35 22 39
Index 1 2 3 4 5
1 2 3
1 A N C
2 C M I
3 E F L
4 P G P
5 Q T W
Since a 2D array is made up of row and column that is why to access an element the row number followed
by the column number is required to make the index. The row index and column index both can start from 0
or from 1.
Grade[3,3] has the value L
Initializing a 1D array: Assume all array elements are given an initial value of 0 (zero). Initializing means
assigning each array cell with the value 0.
DECLARE Num : ARRAY[1:50] OF INTEGER
FOR Index ← 1 TO 50
Num[Index] ← 0
NEXT Index
Page 2 of 6
Write down the pseudocode that will assign each element of a 2D array as 0. The name of the array is
“Number” and the array is made up of 20 rows and 5 columns which can hold only whole numbers. The
starting index is 1. Declare the array before use.
DECLARE Number : ARRAY[1:20, 1:5] OF INTEGER
FOR Row ← 1 TO 20
FOR Column ← 1 TO 5
Number[Row, Column] ← 0
NEXT Column
NEXT Row
We usually need nested loop (a loop inside another loop) to manipulate a 2D array.
Note that the Name and Weight of First Student corresponds to the element StudentName[1] and
StudentWeight[1] respectively and the Name and Weight of Thirtieth Student correspond to the element
StudentName[30] and StudentWeight[30] respectively.
Page 3 of 6
Displaying values from 1D Array
Here is how to display (output) 30 values from two different arrays using FOR...TO...NEXT loop (which
were stored in previous section). The values represent Name of 30 students in one array and Weight of 30
students in another array.
FOR X ← 1 TO 30
PRINT “Student Name: ”, StudentName [X]
PRINT “Student Weight: ”, StudentWeight [X]
The above two statements can be combined into one statement as below:
PRINT “Student Name: ”, StudentName [X], “ and weight is: ”, StudentWeight [X]
NEXT X
Note that for managing Arrays, FOR loop is the most preferred loop structure but other loop structure can
also be used.
Note that for managing Arrays (both 1D and 2D), FOR loop is the most preferred loop structure but other
loop structure can also be used.
Write down the pseudocode that has temperature of a month (30 days) already stored in a 2D array. In each
day temperature is taken for each hour. Find the total for each day and also for all 30 days. The name of the
array is Temperature.
AllDaysTotal ← 0
FOR Day ← 1 TO 30
IndividualDayTotal ← 0
FOR Hour ← 1 TO 24
IndividualDayTotal ← IndividualDayTotal + Temperature[Day, Hour]
NEXT Hour
OUTPUT “The day total for day ”, Day, “is ”, IndividualDayTotal
AllDayTotal ← AllDaysTotal + IndividualDayTotal
NEXT Day
OUTPUT “The total temperature for all 30 days is ”, AllDaysTotal
Page 4 of 6
A 2D array named Marks is already having marks for some students. The array is made up of 10 rows and 3
columns. Find the total of each row separately and then find the grand total of all the rows.
GrandTotal ← 0
FOR Row ← 1 TO 10
RowTotal ← 0
FOR Column ← 1 TO 3
RowTotal ← RowTotal + Marks[Row,Column]
NEXT Column
OUTPUT “The row total of row number ”, Row, “is ”,RowTotal
GrandTotal← GrandTotal + RowTotal
NEXT Row
OUTPUT “The grand total is ”, GrandTotal
Page 6 of 6