Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

CHAPTER 8:

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]

Variable vs 19.90 Array index/


subscript/
[0] 236.50
element
Array position

[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

Initializing/ set month[1] = "Jan"


month[2] = "Feb"
# Initialize the array month[ ] with 12 elements
month = array with 12 elements

the elements month[3] = "Mar"


month[4] = "Apr"
#initialize array value
values = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct',
of an array month[5] = "May"
month[6] = "Jun" or
'nov', 'dec']

month[7] = "Jul" # to set data value into array month


month[8] = "Aug" DO index from 1 to 12:
month[index] = values[index] #Adjusting the index to match the values array
month[9] = "Sep"
index += 1
month[10] = "Oct" ENDDO
month[11] = "Nov"
month[12] = "Dec“ END
END
Example 2 – initialize the elements of an array ‘number’
Set number[ ] to {3, 6, 50, 100}

# Initialize the array number[ ] with 4 elements


Array number = array with 4 elements number = array with 4 elements
Operation-1 : # Values to be set in the array
# Setting values for each element
Initializing/ set/ number[1] = 3 values = [3, 6, 50, 100]
get the number[2] = 6 or
# To set array data
elements of an number[3] = 50
DO index from 0 to 3:
number[4] = 100
array number[index] = values[index]
index += 1
ENDDO
Array  Simple algorithms that manipulate arrays
 The following algorithm involve the simple
Operation-2: manipulation of an array written in a DO loop
Processing  In this example, each element of the array is
the elements accumulated into a variable called ‘sum’.
When all elements have been added, the
of an array variable ‘sum’ is printed
Example 3 - Find the sum of the elements of an array from the value
that has been assigned.
Set number [5] to {2, 4,5,6,6}

Find_sum_of_elements
#set the array size
number = array with 5 elements
Array
Operation-2: Set sum to zero

Processing the values = [2, 4, 5, 6, 6]

elements of an #to set the array data

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

Input Processing Output


Example -6: 20 exam Prompt for 20 scores 20 exam scores,
Process exam scores Get 20 scores Average score
Compute average
scores score
Display 20 scores
Display average score
Control structures required
1. An array to store the exam scores – that is,
‘scores’
Example - 2. An index to identify each element in the
array
6(a): Process 3. A DO loop to accept the scores
exam scores 4. Another DO loop to display the scores to
the screen
Process_exam_scores
#to set array scores size
scores = array with 20 elements

Set totalScore to zero


#to get data value for array name
Example - ‘scores’
DO index = 1 to 20
6(b): Process Prompt operator for score
Get scores [index]
exam scores totalScore += scores[index]
index += 1
ENDDO
Solution algorithm 
averageScore = totalScore
#to display data / 20 name
value for array
‘score’
DO index = 1 to 20
Display scores[index]
index += 1
ENDDO

Display averageScore
In c++ coding

Example 6(c) -
Process exam
scores

In python coding? Will do in Lab Session


Problem (a):
 Every day, a weather station receives 15 temperatures
expressed in degrees Fahrenheit. A program is to be
written that will accept each Fahrenheit temperature,
Try this: convert it to Celsius and display the converted
temperature to the screen. After 15 temperatures
[Exercise Ch8] have been processed, the word “All temperatures
processed” are to be displayed on a screen.
 Solve this problem using Array
Formulae to convert temperature Celcius:
temp_c = (temp_f – 32) * 5/9
Problem (b):
 Design a pseudo code that will read an array of
100 integer values, calculate the average
Try this: integer value, and count the number of
[Exercise Ch8] integers in the array that are greater than the
average integer value. The algorithm is to
display the average integer value and the count
of integers greater than the average.
 Data that would normally be displayed in a table format usually
falls into this category.
 For example, scores for all of the students in a class would
naturally be stored in a table, with a list of students along the left
side of the table and a list of assignments along the top of the
Two- table.
 To store data for a class with 5 students (I've picked a small class
dimensional size to keep the figures small) and 3 assignments, we would
array declare a 2-dimensional array like this:
scores(5)(3)

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??

You might also like