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

Arrays 1

An array is a list of variables of the same data type stored in contiguous memory locations. Arrays allow storing multiple elements of the same type using a single name. Arrays can be one-dimensional (1D) or two-dimensional (2D). 1D arrays use a single index while 2D arrays use two indices to access elements. Arrays must be declared before use by specifying the data type and size. Elements can be initialized and accessed using indices. Loops are commonly used to populate, manipulate and display array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Arrays 1

An array is a list of variables of the same data type stored in contiguous memory locations. Arrays allow storing multiple elements of the same type using a single name. Arrays can be one-dimensional (1D) or two-dimensional (2D). 1D arrays use a single index while 2D arrays use two indices to access elements. Arrays must be declared before use by specifying the data type and size. Elements can be initialized and accessed using indices. Loops are commonly used to populate, manipulate and display array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Science

Teacher: Maruf Ahmed


Arrays:
By definition, an array is a list of variables with the same data type and a name (identifier). For example, you
could have an array of integers or an array of string or an array of anything that has a defined data type.
When we work with a single item, we only need to use one variable. However, if we have a list of items
which are of similar type to deal with, we need to declare an array of variables instead of using a variable for
each item.
For example, if we need to enter one hundred names, it is difficult to declare 100 different variables; this is
a waste of time and efforts. So, instead of declaring one hundred different variables, we need to declare
only one array. We differentiate each item in the array by using subscript, the index value of each item, for
example name[1], name[2], name[3] etc., which will make declaring variables streamline and much more
systematic.
Why do we use arrays?
easy to handle
less lines of codes required
reduces the number of variables
arrays have an index which identifies each stored element
programs are easier to debug
The important characteristics of an array are:
Each element has the same data type (although they may have different values).
The entire array is stored contiguously in memory (that is, there are no gaps between
elements).
Arrays can be one dimensional (1D) or two dimensional (2D). A one-dimensional array is called a vector;
a two-dimensional array is called a matrix or table.
Dimension: The dimension is the number of indices required to access an element.
Index: The index is the position of the element in an array
Example of dimension and index in an array:
For example A[15] is the 15th element of a one-dimensional array.
For example B[10][5] is the element in the 10th row and 5th column of a two-dimensional array.

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

Note: the first element of an array can have subscript/index as 0 or 1.


Pseudocode to declare 2D array:
o DECLARE <Identifier name> : ARRAY[Lowerbound : Upperbound, Lowerbound :
Upperbound] OF data type
o The first part is for row size and the second part is for column size
o DECLARE Grade : ARRAY [1:5,1:3] OF CHAR
The above pseudocode statement declares an array with 5 rows and 3 columns, all of which are of
CHAR

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

Data type can be any of INTEGER, REAL, STRING, CHAR, BOOLEAN

Remember that to manipulate 1D array we use loop

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.

Storing / populating values into two different 1D Arrays


Here is how to take input of name and weight of students in two different arrays. The values represent
Name of 30 students in one array and Weight of 30 students in another array.
DECLARE StudentName : ARRAY [1:30] OF STRING
DECLARE StudentWeight : ARRAY [1:30] OF REAL
FOR Count ← 1 TO 30
PRINT“Enter Student Name: ” These two statements can be written as
INPUT SName one instruction as below:
StudentName [Count] ← SName INPUT StudentName[Count]
PRINT “Enter Student Weight: ”
INPUT SWeight These two statements can be written as
one instruction as below:
StudentWeight [Count] ← SWeight
INPUT StudentWeight [Count]
NEXT Count

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.

Storing / populating values in a 2D Array


Here is how to take input of 50 marks which are all whole numbers in a 2D array which is made up of 10
rows and 5 columns.
DECLARE StudentMark : ARRAY [1:10,1:5] OF INTEGER
FOR Row ← 1 TO 10
FOR Column ← 1 TO 5
OUTPUT “Please enter student mark: ”
INPUT StudentMark[Row,Column]
NEXT Column
NEXT Row

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.

Displaying values from 2D Array


Here is how to display (output) all the values from a 2D array named StudentMark which is made up of 10
rows and 5 columns.
FOR Row ← 1 TO 10
FOR Column ← 1 TO 5
OUTPUT StudentMark[Row,Column]
NEXT Column
NEXT Row

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

LINEAR SEARCH in a 1D array


Searching in one-dimensional array: Suppose an array has 50 items already stored in it. You have to take
item number as input from a user and check if the item is in the array. If the item is found then display the
index number where it was found. If not found then display a message. Assume that the name of the array is
ItemNumber.
Solution using WHILE loop
Search ← 1
IsFound ← FALSE //This is a Boolean variable and used as a FLAG. This variable has been
initialized as FALSE and during the execution of the program this value will be changed as TRUE based
on a condition
PRINT “Please enter the item number you are looking for: ”
INPUT SearchItem
WHILE IsFound = FALSE AND Search <= 50 DO
IF ItemNumber [Search] = SearchItem
THEN
IsFound ← TRUE
PRINT “The item found at location ”, Search
ELSE
Search ← Search + 1
ENDIF
ENDWHILE
IF IsFound = FALSE
THEN
PRINT “The item was not found”
ENDIF
Page 5 of 6
Solution using REPEAT loop
Search ← 1
IsFound ← FALSE
PRINT “Please enter the item number you are looking for: ”
INPUT SearchItem
REPEAT
IF ItemNumber[Search] = SearchItem
THEN
IsFound ← TRUE
PRINT “The item found at location ”, Search
ELSE
Search ← Search + 1
ENDIF
UNTIL IsFound = TRUE OR Search > 50
IF IsFound = FALSE
THEN
PRINT “The item was not found”
ENDIF

What is a flag variable?


A flag variable, in its simplest form, is a variable you define to have one value until some condition is true,
in which case you change the variable's value.
Flag variable is used as a signal in programming to let the program know that a certain condition has met. It
usually acts as a Boolean variable indicating a condition to be either true or false.

Page 6 of 6

You might also like