0% found this document useful (0 votes)
11 views49 pages

CH8 Processing Arrays

Chapter 8 covers the processing of arrays, including one-dimensional and two-dimensional arrays, and their applications in programming. It discusses data structures, the types of arrays, methods for entering and printing data, and techniques for accumulating elements. The chapter also introduces table look-up techniques and the pointer technique for handling frequency distributions and cross-tabulations.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views49 pages

CH8 Processing Arrays

Chapter 8 covers the processing of arrays, including one-dimensional and two-dimensional arrays, and their applications in programming. It discusses data structures, the types of arrays, methods for entering and printing data, and techniques for accumulating elements. The chapter also introduces table look-up techniques and the pointer technique for handling frequency distributions and cross-tabulations.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter 8:

Processing Arrays
SW 100: Principles of Programming and Problem Solving

Dr.Hanin 1
Abdulrahman
Overvie
•wArrays
• One-Dimensional Arrays
Entering Data, Printing, Accumulating the Elements
• Two-Dimensional Arrays
Entering Data, Printing, Accumulating the Rows and Columns
• Multidimensional Arrays
• Table Look-Up Technique
Sequential Search, Binary Search
• The Pointer Technique
Frequency Distribution, Cross-Tabulation

Dr.Hanin 2
Abdulrahman
Objectives
1. Develop solutions using one-
dimensional and two-dimensional
arrays.

2. Develop solutions requiring table


lookups, sequential and binary
searches, frequency distributions, and
cross- tabulations.
Arrays
• Data are stored in the computer in a logical way in order to
find values as quickly as possible.
• The way data are stored is called the structure of the data,
hence the term data structures.
• The only data structure you have used so far in this text is the
single variable. Using this data structure, a single value is
stored in a single named location. This is the simplest of all
data structures.
Definition from
Dictionary of Algorithms and Data Structures
(National Institute of Standards & Technology ,USA)
http://www.nist.gov/dads/

•Data Structure
An organization of information, usually in memory, for better
algorithm efficiency, such as queue, stack, linked list, heap, dictionary,
and tree, or conceptual unity, such as the name and address of a person.
It may include redundant information, such as length of the list or
number of nodes in a subtree.

Note: Most data structures have associated algorithms to perform


operations, such as search, insert, or balance, that maintain the
properties of the data structure.
Arrays (cont…
• Another type of data structure is the array. It is often advantageous for
a user to store several values for the same variable in the internal
memory of the computer because it decreases processing time.
• This multiple storage means there has to be more than one memory
location in the computer for each variable name.
• When more than one memory location is designated for a single
variable, it is called an array.
• Sometimes these variables are called subscripted variables. This term
comes from mathematics, where subscripts are used to designate
multiple values.
Arrays (cont…
• Arrays are also called tables, a term from business, where data are
often put into tables.
• For the purposes of this book, array will be used because it is universal
and is becoming the most common term.
• Arrays are used in the problem-solving process. A programmer who
has many values of data of the same kind—many ages, temperatures,
names, or the like—can store them in the computer in the form of an
array, which makes them easier to read and use.
Arrays (cont…

• A programmer uses arrays when more than one value of a variable is used
in the solution—for example:
1. when calculating the percentage of each store’s sales out of the total company’s sales.
2. when calculating each student’s test grade
The store sales or the student grade would be the variable with more than one value—a value for each store or for each student.

• If the user does not need to use the data more than once in the
solution, then there is no need for the programmer to use arrays. Their
overuse is a common mistake for beginning programmers.
Arrays (cont…

• The programmer tells the computer how many memory locations to


save for an array through a special instruction or within the definitions
of the data types. In many languages this is called dimensioning the
variable.
• The number of memory locations to be saved is equal to or greater
than the number of locations used in the solution. If it is less than the
number of locations used, an error will result. For problem-solving
purposes, this number is part of the annotation of the flowchart.
Types of Arrays

There are basically two types of arrays:


• Static Array: In this type of array, memory is allocated at compile time
having a fixed size of it. We cannot alter or update the size of this
array.
• Dynamic Array: In this type of array, memory is allocated at run time
but not having a fixed size.

Dynamic arrays are more flexible and use less memory space than static
arrays, but are usually more time consuming during processing.
Array Elements

• Each array memory location is called an element and is given a number


or numbers corresponding to the position of the location in the array.
• This number is a reference number relative to the location of the first
value of the array.
• The name of the element has two parts.
 The first part is the variable name;
 the second part is the reference number, also called the index
number, an element number or subscript in parentheses.
Base-Zero Versus Base- One Arrays

Because computers are zero-based, for counting purposes, many programming


languages are also zero-based.
This means that the first array element is numbered zero,
and not one; it is a base-zero system. In this way, the computer adds the element number
to the memory location number of the beginning box to locate the needed value.
One-Dimensional Arrays
• The simplest array structure is the one-dimensional array.
• This array consists of one column of memory locations. In the next
Figure, there is a one-dimensional array named Age.
• the reference names are written as Age(1), Age(2), Age(3), and so
forth. The number in the parentheses is a reference number only and
can be a constant, a variable, or an expression.
• In most languages the reference numbers must be of the integer data
type.
One-Dimensional Arrays
Parallel arrays (one-Dimensional
Arrays)
Parallel arrays are two or more arrays in which values in the same
elements relate to each other. For example, in the next Figure there are
three arrays.
The first array contains area numbers, the second array contains
temperatures for day 1, and the third, temperatures for day 2.
Parallel arrays (one-Dimensional
Arrays)
Entering Data in one-Dimensional
Arrays
• When you enter data into an array, or load an array, you use a loop.
• If you know the number of elements, then use the automatic-counter
loop.
• If you do not know the number of elements, then use an indicator
code with the Repeat/Until or the While/WhileEnd loop. Use a
counter to keep track of how many elements are entered into the
array.

Figure 8.4 shows the loop process for entering the data into an array.
Entering Data Using automatic
counter loop
Entering Data Using Repeat/Until
Loop
Entering Data Using
While/WhileEnd loop
Printing (one-Dimensional Arrays)

• After the array is loaded, it can be used in other calculations or printed.


You write the algorithm to print an array by using the automatic-
counter loop since the number of elements in the array is now known
(see Figure 8.5). The array will be printed one element at a time down
the page.
• The headings to the columns have to be printed before the processing
starts into the automatic-counter loop.
Printing (one-Dimensional Arrays)
Accumulating the Elements (one-
Dimensional Arrays)
• Often you need the sum of the elements in an array. Use the
accumulation instruction to total the sum (see Figure 8.6). Initially, set
the variable containing the value of the sum to zero.
• The accumulation instruction tells the computer to add the value of
the element Array(Element) to the old value of the sum (Sum)  (Sum
= Sum + Array(Element)  sum+=Array(Element). and to store the
result in the sum memory location (Sum).
• The value of the element number (Element) changes as the loop
counter progresses from one element to the other during summing.
Accumulating the Elements
Putting It All Together (one-Dimensional Arrays)
Example

Problem: The president of the (Too Little Variety Stores) would like to
know each store’s percentage of the total sales of the corporation. There
are 15 stores altogether.

• Figure 8.7 shows the complete solution to this problem.


• Figures 8.7a–c shows the Problem Analysis Chart, the Interactivity Chart, the IPO Chart, the coupling chart and the data dictionary.
• Figures 8.7d–g show the algorithms and flowcharts for each module.

 The Control module processes each of the modules, Init, Read, Calc, and Print.
 The Sum is set equal to zero in the Init module.
 The Read module enters the values of the sales per store into the Sales array. The value of Element becomes the store number.
 In the Calc module, the sum of the sales for all 15 stores is calculated through the use of a loop and the accumulation instruction. The
next loop calculates the value of the percentage for each store and places that value in the Percent array.
 The Sales array and the Percent array are parallel arrays since each element corresponds to the same store.
 The values of the store number, the Sales, and the Percent are printed, element by element, in the Print module.
ach d
e s e , an
e ss alc
roc d, C
p
le Rea
u ,
od Init
o l m les,
n tr du
Co mo
e
Th f the .
 o int
Pr

l to .
q ua ule
et e od
s m
is Init
S um the
e
Th ro in
 ze
the
e rs ore
e nt r st
ule s pe he s
od ale y. T ome
d m e s rra ec
R ea f th les a nt b
he es o Sa me er.
T u e e b
 val o th f El num
o
int lue ore
va e st
th
l e , t he
odu a l l 15
m
alc ales for
e C
th es d
 In m of th lculate a loop
su s is ca
e u s e of n
stor gh the mulatio loop
u t
thro he accu he nex f
t T o
and uction. e value ch
r a
inst lates th ge for e
u t
calc ercenta ces tha rray.
p a a
the and pl ercent
e P
stor in the
e
valu
b er,
n um e
tore t ar in
th e s ercen ent,
s of the P elem
a lue nd t by
e v s, a en
Th le m .
 he Sa d, ele odule
t
rinte int m
p Pr
th e
e rcent
d the P nce
rray an rays si to
S ales a rallel ar ponds
e a s
 Th ay are p nt corre
arr eleme e.
r
each ame sto
s
the
Two-Dimensional Arrays

• A two-dimensional array is a block of memory locations associated


with a single memory variable name and designated by row and
column numbers.
• Each element number is written as Array (Row #, Column #). The row
number is always first and the column number second.
• As in one-dimensional arrays, the row and column numbers can be
constants, variables, or expressions, and they are of the integer data
type.
• Whatever processing takes place, the row and column numbers always
remain in the same positions in the parentheses.
• As with one-dimensional arrays, you can change the values in a two-
dimensional array by using the assignment instruction.
Two-Dimensional Arrays (cont…

• Two-dimensional arrays are used for tables of values. They can replace
parallel arrays if the data are of the same data type in all of the arrays.
• In Figure 8.8, the two-dimensional array Array is shown. This figure
shows the general form of a two-dimensional array, and of reference
names (including the variable name and the row and column
numbers), for each location.
• For example, Array(3,2) is found at the intersection of row 3 and
column 2.
Two-Dimensional Arrays (cont…
Entering Data (Two-Dimensional Arrays)

• You load a two-dimensional array with nested loops (see Figure 8.9).
The data are normally loaded row by row.
• When you load the data row by row, the outer loop represents the
row, and the inner loop represents the column. This order of the loops
allows the row number to remain constant while the column number
varies.
• In Figure 8.9, compare the data block to the array block. In the
flowchart, notice that the first four numbers in the data block are read
into the first row of the array, the second four into the second row, and
the last four into the third row. The array is now loaded with the
numbers 1 to 12.)
Printing (Two-Dimensional Arrays)

• After the array is loaded, it can be used in calculations, or it can be


printed. As in loading, use nested loops to print a two-dimensional
array.
• Normally, the array is printed row by row. For example, in Figure 8.10
the outer loop (Row loop) is the row loop and the inner loop (Column
loop) is the column loop.
• Notice that the flowchart is designed so that the element is printed
without a carriage return. The carriage return is executed after a row is
printed, that is, after the end of the column loop.
Printing (Two-Dimensional Arrays)

• Place the Cursor Return instruction between the two LoopEnd


instructions.
• The column headings are printed before either loop is executed. Each
row heading is printed between the two beginning loop instructions.
• Follow the execution of the loop in Figure 8.10 to see how the array is
printed.
 Notice that the column headings are printed first.
 Then the row loop (Row loop) prints row 1 and continues
to print through row NRows, the total number of rows.
 Within each row, the row heading must be printed first.
 Then, through the use of the Column loop, each element
of the row, designated by the column number (Column), is
printed without a cursor or carriage return.
 Once the elements of the row are printed, the cursor or
carriage has to be returned before the next row can be
printed.
Accumulating the Rows and
Columns
•(Two-Dimensional Arrays)
In many solutions you need a total of rows, columns, and the grand
total of all elements in a two-dimensional array, and as in the one-
dimensional array, you use the assignment statement to do so.
• In the solution, you can use a row following the last row of the array
for the sums of the columns, and a column to the right of the last
column for the sums of the rows (see Figure 8.11).
• For the grand total, use the intersection of the last row and the last
column. This placement of row, column, and grand totals facilitates
printing them in the printout of the array.
Accumulating the Rows and
Columns
•(Two-Dimensional Arrays)
In the array in Figure 8.11, the fourth row is used for the sum of each
column, and the fifth column is used for the sum of each row.
• The element in the lower right-hand corner—Array(4,5)—holds the grand
total. In the solution, the sum of both the rows and columns can be done in
the same nested loop.
• It does not make any difference if the sum of a single row is completed
before the start of the sum of the next row, or if the sums of all rows are
done at the same time, column by column.
• Notice in Figure 8.11 that the final sum is the same if you accumulate a
whole row at one time or all of the rows at the same time.
• Either way, the sum of the first row is 10, the sum of the second row is 26,
and the sum of the third row is 42. The same is true of the columns.
Accumulating the Rows and
Columns
•(Two-Dimensional Arrays)
In Figure 8.12, the algorithm and the flowchart are shown for summing
the rows and columns of the array in Figure 8.11.
• Notice the differences between the assignment instructions for summing
the rows, the columns, and the grand total.
• The only difference is in the reference numbers of the array elements
containing the sums.
• The grand total is the sum of the last column since the sum of the rows is
complete at the end of the inner loop.
Accumulating the Rows and
Columns
•(Two-Dimensional
In the instruction to sum theArrays)
rows in Figure 8.12, the element numbers are
represented by the variable Row for the row number, and the expression
(NColumns + 1)for the column number containing the sum of the rows.
• In the instruction to sum the columns, the element numbers are represented by
the expression (NRows + 1)for the row containing the sum of the columns, and the
variable Column for the column number.
• The element numbers of the box containing the grand total, at the intersection of
the sums of the rows and the columns, are (NRows +1 and NColumns + 1.) When
the rows are summed in the outer loop, they are summed one row at a time; the
columns are summed when all the rows have been processed.
• The summation of the grand total is placed between the two LoopEnds because it
is the summation of the sums of the rows.
• The elements to be accumulated for the grand total are the row sums in
Array(Row, NColumns + 1).
1 2 3 4 5

1 10 20 30 40 100
2 100 200 300 400 1000
3 1000 2000 3000 4000 ..
4 70 77 777 7777 ..
5 80 88 888 8888 ..
6 90 99 999 9999 ..
7 10 +100+… 20+200+.. 30+300+.. 40+400+..

Array[1,5]= Array[1,5]+ Array[1,1]  0=0+10 Array[2,5]= Array[2,5]+ Array[2,1]  0=0+100


Array[7,1]= Array[7,1]+ Array[1,1]  0=0+10 Array[7,1]= Array[7,1]+ Array[2,1]  10=10+100

Array[1,5]= Array[1,5]+ Array[1,2] 10=10+20 Array[2,5]= Array[2,5]+ Array[2,2] 100=100+200


Array[7,2]= Array[7,2]+ Array[1,2] 0=0+20 Array[7,2]= Array[7,2]+ Array[2,2] 20=20+200

Array[1,5]= Array[1,5]+ Array[1,3] 30 = 30+30 Array[2,5]= Array[2,5]+ Array[2,3] 300 = 300+300


Array[7,3]= Array[7,3]+ Array[1,3] 0=0+30 Array[7,3]= Array[7,3]+ Array[2,3] 30=30+300

Array[1,5]= Array[1,5]+ Array[1,4] 60= 60+40100 Array[2,5]= Array[2,5]+ Array[2,4] 600= 600+4001000
Array[7,4]= Array[7,4]+ Array[1,4] 0=0+40 Array[7,4]= Array[7,4]+ Array[2,4] 40=40+400

You might also like