Ch8 DataManagement Array
Ch8 DataManagement Array
DATA
MANAGEMENT
IAS1123 Programming
Methodology
Chapter Outline
Introduction to array
Multi Dimensional Array
Array is a data structure (collection of data which have same data
type).
Allows us to group and store variables with the same data type
together.
Usually, a variable can hold ONE value in a single variable.
Introduction However, array allow to store many values in one variable, each
values is known as ELEMENT and has its own ARRAY INDEX (or
SUBSCRIPTS).
A subscript is a number that indicated the position of a particular
array element that is being used.
Array name : Price
Size of array : 3
Variable Array Type : Float
float Price float Price [3]
[1] 55.90
Usually,
index start
with 0
[2] 6.90
VE
RTI
Price [4] CA
Price [0] 236.50 Element index 0 L
Array index Price [1] Element index 1
55.90
[subscript]
Price [2] 6.90 Element index 2
Price [3] 24.30 Element index 3
Arrangement OR
of array
Price [4]
Price [0] Price [1] Price [2] Price [3]
236.50 55.90 6.90 24.30
HORIZONTAL
Element Element
index 0 index 2
Element
Element
index 3
index 1
Arrays are one of the most powerful programming tools
available
Introduction Provide the programmer with a way of organizing a
collection of homogeneous data items (items that have
to an array the same type and same length) into a single data
processing structure
An array is a data structure that is made up of a number
of variables all of which have the same data type
Introduction Elements in the array are distinguished from one another
by the use of an index or subscript, enclosed in parentheses,
to an array following the array name.
processing Example: ‘price[2]’
The most typical operations
performed on arrays are:
Introduction to • Initialize / Loading a set of initial
an array values into the element of an array
processing • Processing the elements of an array
• Ex: find sum, average, etc
• Writing out the contents of an array to
a report
Introduction to The elements of an array are processed in sequence,
starting with the first element.
an array This can be accomplished easily in pseudocode with
processing either a DO loop or a DOWHILE loop
Array Because an array is an internal data structure, initial
Operation-1: values must be placed into the array before any
information can be retrieved from it
Initializing the These initial values can be assigned to the elements
elements of an of the array as constants, or they can be read into
the array from a file
array
Loading constant values into an array
This method should only be used when the data in the array is unlikely to be
changed
To initialize such an array called ‘month’, which contains 12 elements all the
same size. Then assign the elements of the array with the name of the
‘month’, one by one as follows:
Example 1- Initialize the names of the 12 months of the year (pseudo code)
Array
Operation-1 : START
# Assigning values to each element START
Find_sum_of_elements
#set the array size
number = array with 5 elements
Array
Operation-2: Set sum to zero
array DO index 1 to 5
number[index] = values [index]
sum += number[index]
index +=1
ENDDO
Print sum
END
Example 4:
The program will accept five (5) number from user. Then, the input number will be
stored in an array name ‘number’, sized of five. The program will find and display
the total sum of the input number.
Find_sum_of_elements
Array #set the array size
number = array with 5 elements
Operation-2: Set totalSum to zero
Processing the
#to get the array data
elements of an DO index = 1 to 5
array Get number [index]
totalSum += number [index]
index += 1
ENDDO
Print totalSum
END
The elements of an array can be
used as accumulators of data, to be
Array written to a report
Operation-3 :
Writing out the
contents of an Writing out the contents of an array
array involves starting with the first
element of the array and continuing
until all elements have been written
This can be represented by a simple DO loop
Write_values_of_array
Array DO index = 1 to
Operation-3: number_of_elements
Writing out/ Print array [index]
print the index += 1
contents of an ENDDO
array END
Example 5 : To display the array element’s value after user input the
number to an array of five ‘numbers’
Array
Operation-3: Write_values_of_array
DO index = 1 to 5
Writing out the Print number [index]
contents of an index +=1
array ENDDO
END
Design a program that will prompt for and receive 20 examination
scores from a mathematics test, compute the class average, and
display all the scores and the average score to the screen
Defining Diagram
Display averageScore
In c++ coding
Example 6(c) -
Process exam
scores
student assignment
To access array:
Scores(0)(0)=85;
Scores(1)(2)=57;
Scores(4)(1)=72;
Arrangement
of two-
dimensional
array
How to read:
row(0) column(0)=85;
row(1) column(3)=57;
row(3) column(1)=72;
Arrangement
of two-
dimensional
array
Loading a two-dimensional array
Two- Two dimensional array is loaded in columns within row order
All the columns for row one are loaded before moving to row
dimensional two and loading the columns for that row, and so on
The reading of a series of values from a file into a two-
arrays dimensional array can be represented by a DO loop within a
DOWHILE loop
QUESTION??