Arrays and Text
Processing
Processing Arrays
◼ Objectives:
▪ Develop problem solutions for the computer using
one-dimensional and two dimensional arrays.
▪ Develop problem solutions requiring table
Arrays
◼ Array is used as a powerful data structure.
◼ Use by programmer when storing many values
of data of same kind.
◼ There are two types of array:
▪ Static Arrays (fixed sized arrays)
Arrays
◼ 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.
◼ Arrays can be defined in:
▪ Base-Zero System
PIAT (One Dimensional Array)
◼ 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 above problem is solved by using one dimensional
Problem Analysis Chart
Given Data Required Results
1. 15 stores
2. Sales per store
Percent of total sales per store
Processing Required Solution Alternatives
Percent (Store) = sales (store)/total sales
Interactivity Chart
Sales
IPO Chart
Input Processing Module Reference Output
1. Sales per store 1. Enter data
2. Set sum to 0
3. Accumulate sales
4. Calculate percent
5. Print output
Read
Init
Calc
Calc
For each store: 1. Store number 2. Sales
Data Dictionary
Item Variable Name
Data Type Module Scope Pseudonyms Error Checks
Sum of sales
Sum Numeric Control, Init, Calc
Local None None
Sales Array Sales Numeric All Local None None
Sales
Percentage
Percent Numeric Control, Calc, Print
Local None None
Element No.
R Numeric Read, Calc, Print
Algorithm & Flowchart for Modules
◼ Control & Init Module
◼ Read Module
◼ Calc Module
Two Dimensional Array
◼ 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:
Loading Two Dimensional Array
◼ Use of nested loops
◼ Data is loaded row by row (outer loop
represents the row and inner loop represents
the column)
◼ Loading a Two Dimensional Array
Accumulating Two Dimensional
Array
◼ Accumulation means total of rows, total of
PIAT
◼ Problem: The president of the Too Little Variety
Stores has collected sales data on the 15 stores in
the corporation for seven days. He would like to
know the total sales for each store for the week,
the total sales for the company for each of the
seven days and the total sales for the company for
Problem Analysis Chart
Given Data Required Results
1. 15 stores
2. Sales for all stores for seven days
1. Total sales for each store for the week. 2. Total sales for the company for each
day.
3. Total sales for the company for the week.
Processing Required Solution Alternatives
1. Summing store sales 2. Summing day’s sales 3. Summing all sales
1. Use two dimensional array for data, one dimensional array for store totals and day’s total, and a single variable for grand total.
Interactivity Chart
Sales
IPO Chart
Input Processing Module Reference
Output
1. Sales per day per store 2. 15 stores
1. Enter data
2. Set array to 0
3. Calculate totals
4. Print output
Read
Init
Calc
1. Total sales for each store for the week. 2. Total sales for each
day for company. 3. Total sales for week
Coupling Diagram
Control
Init Read Calculate Print
Sales
Sales
Data Dictionary
Item Variable Name
Data Type Module Scope Pseudonyms Error Checks
Sales Array Sales Numeric All Local None None
Row No. R Numeric Read,
Calc, Print Local None None Column
No. C Numeric
Read,
Algorithm and Flowchart for all
Modules
◼ Control Module
◼ Init Module
◼ Read Module
◼ Calculate Module
Multidimensional Array
◼ An array with three or more than three dimensions are
called multidimensional array.
◼ These array are used to store and process all data in
one data structure.
◼ The recommended variables names for the indexes of a
three dimensional array are R for rows, C for columns,
D for depth and for fourth dimension V for volume is
Table Look-Up Technique
◼ Arrays are used to look up another value in a table using a given value.
◼ In one dimensional array the element number is used as the given value.
Table Look-Up Technique
◼ Eg: find the number of days in a month given
the month number.
◼ Here we use the element number as the month
number.
Sequential Search
◼ Sometimes the element number is known but
the value of the element is unknown.
◼ Sequential search is used to find the value of
the element.
Binary Search
◼ It the fastest method of finding the desired element of
an array.
◼ This technique involves comparing the mid-element of
all or part of the array. If it is the result then stop
otherwise the algorithm checks whether the value is
lower or higher than the middle value.
◼ The boundaries are again reset to select a new section
Binary Search
◼ The above procedure is repeated till the
desired value is found.
The Pointer Technique
◼ Arrays are also used to specify the value of an element
in one array as the element number in another array.
◼ This technique is called pointer technique because the
value of element in first array points to the element in
second array.
◼ Used in frequency distribution and cross tabulation.
Frequency Distribution
◼ Problem: Using grade scale of 1 to 10, design a solution to find the number of students out of a class of 30 who received each grade on a test.
◼ Solution:
▪ Define array from which frequency distribution is to be calculated ( ie: F)
▪ Define second array containing the grades for the students ( ie: grade )
Grades and Frequency distribution
Element Grades
Algorithms and Flowchart for
Frequency Distribution
◼ Control Module
◼ Init and Read Module
Cross Tabulation(CT)
◼ In cross tabulation array each one value points to the row and other points to the column.
◼ The data for CT problem is comes from questionnaires.
◼ CT show the number of people who gave the same responses to 2 questions from the
Contd…
◼ Frequency distribution shows the number of people who gave the same answer to one
question.
◼ 1(Yes) 2(no)
32 25
51 42
Cross Tabulation
◼ Problem: A questionnaire has been sent out by a company that conducts a polls. The company needs a program to calculate the cross
tabulation for any two items on the
Steps involved
1. The init module initializes the variables.
2. The Enter data module enters the questionnaire data.
3. The choose option module chooses the two items for the
cross tabulation.
4. This loop repeats the cross tabulation until no more cross
tabulation are desired by the user.
5. The calc module calculates the cross tabulation
6. The print module prints the results of the cross tabulation.
7. The init module processes at this point reinitializes the
Steps involved
8. This choose option module chooses the two items for the next
cross tabulation
9. This nested automatic counter loop set the elements of the cross
tabulation array to zero.
10. These loops are set so that the rows (R ) are the questionnaires
(1 to 150) and the columns ( C ) are the items (1 to 5).
11. This instruction enters the questionnaire data, one element at a
time.
12. This decision instruction checks to see if the column number is
Steps involved
13. These instructions clear the screen and print the menu of
the questionnaire items.
14. This loop enters the first item number (option 1) for the
cross tabulation option. It repeats until the number entered is within the limits of the options of the menu.
15. This instruction initializes option 2 to prepare for the next
loop.
16. This loop enters the second item number (option 2) and
repeats until the answer is within the limitations of the number of items (1 to 5) and is not equal to option 1.
17. Rmax is the maximum number of rows used in the cross
tabulation. It is set to zero since the maximum number of rows has to be greater than zero.
18. Cmax is the maximum number of columns in the cross
Steps involved
◼ This loop is set up for the number of questionnaire (150)
◼ R is the pointer to the row of the cross tabulation array. The value comes from the value in the option1 column for that questionnaire. Option 1 is the first item number entered by the user.
◼ C is the pointer to the row of the cross tabulation array. The value comes from the value in the option2 column for that questionnaire. Option 2 is the first item number entered by the user.
◼ This instruction increments the element in the cross tabulation array specified by te pointers R and C.
Steps involved
24. This instruction prints column headings.
25. This loop specifies the number of rows to
print, determined by Rmax.
26. This instruction prints the row heading each
time through loop.
27. This loop specifies the number of columns to
print, determined by Cmax.
28. This instruction prints an element without a
carriage return, as specified by R and C.
29. This instruction returns the carriage after a
PIAT on Cross Tabulation
◼ Problem: A company has data on the number of days each employee was absent during the previous year. The data also include gender( 1 = female, 2 = male),
Problem Analysis Chart
Given Data Required Results
Per employee:
Gender ( 1 = F, 2 = M )
Pay type (1 = S, 2 = H, 3 =C)
Education ( 1= N, 2 = HS, 3 = C, 4 = B, 5 = G)
Age group ( 1= <30, 2= 30 to 50, 3 = >50) # of days absent.
Cross – tabulate for average number of days absent
Processing Required Calculate of average using cross tabulation technique.
Interactivity chart
Absent
Init Enter Calc Print
IPO Chart
Input Processing Module Reference
Output
For each employee • Gender
• Pay type
• Education
• Age group
• # of days absent
1. Set cross tab array and counter array = 0
2. Enter data 3. Print menu
4. Choose options 5. Calculate cross
tabulation
6. Print cross tab array
7. Repeat 3-7 until option 1 = 0
Init Enter Menu Menu Calc Print
Algorithms and Flowcharts
◼ Control Module
◼ Init Module
◼ Menu Module
◼ Enter Module
◼ Calc Module
Array Order Reversal
◼ Problem: Rearrange the elements in an array so that they appear in reverse order.
Problem Analysis Chart
Given Data Required Results
1. Elements in an Array 1. Reversed elements of array
Processing Required Solution Alternatives
1. Enter elements.
2. Calculate number of exchanges 3. Reverse the elements
IPO Chart
Input Processing Module Reference
Output
1. Elements of array 1. Enter the elements of array.
2. Calculate r
3. Exchange elements of array
4. Print reverse array
Read
Reverse Reverse
Algorithm
Loop i : 1 to n
Enter the elements a[i]
Loop End r = n/2
Loop i : 1 to r
t = a[i];
a[i] = a[n-i+1]; a[n-i+1] = t;
Loop end
Loop i : 1 to n
Print the elements a[i]
Finding maximum number in an
Array
◼ Problem: Find the maximum number in a set of n numbers.
Algorithm
Loop i to n
Enter the elements of array a[i]
Loop End
i = 1
max = a[i];
Loop i = 2 to n
If a[i] > max then max = a[i];
Partitioning an Array
◼ Problem: Given a randomly ordered array of n elements, partition the elements into two
subsets such that elements <= x are in one
subset and elements > x are in the other subset.
28 26 25 11 16 12 24 29 6 10
12 6 10 11 16 28 24 29 26 25
Random Set
A partitioned solution
<=1 7