CH8 Processing Arrays
CH8 Processing Arrays
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.
•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.
• 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…
Dynamic arrays are more flexible and use less memory space than static
arrays, but are usually more time consuming during processing.
Array Elements
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)
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.
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
• 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)
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,4] 60= 60+40100 Array[2,5]= Array[2,5]+ Array[2,4] 600= 600+4001000
Array[7,4]= Array[7,4]+ Array[1,4] 0=0+40 Array[7,4]= Array[7,4]+ Array[2,4] 40=40+400