• No results found

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:

N/A
N/A
Protected

Academic year: 2020

Share "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:"

Copied!
52
0
0

Loading.... (view fulltext now)

Full text

(1)

Arrays and Text

Processing

(2)

Processing Arrays

◼ Objectives:

▪ Develop problem solutions for the computer using

one-dimensional and two dimensional arrays.

▪ Develop problem solutions requiring table

(3)

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)

(4)

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

(5)

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

(6)

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

(7)

Interactivity Chart

Sales

(8)

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

Print

For each store: 1. Store number 2. Sales

(9)
(10)

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

(11)

Algorithm & Flowchart for Modules

◼ Control & Init Module

◼ Read Module

◼ Calc Module

(12)

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:

(13)

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

(14)

Accumulating Two Dimensional

Array

◼ Accumulation means total of rows, total of

(15)

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

(16)

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.

(17)

Interactivity Chart

Sales

(18)

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

Print

1. Total sales for each store for the week. 2. Total sales for each

day for company. 3. Total sales for week

(19)

Coupling Diagram

Control

Init Read Calculate Print

Sales

Sales

(20)

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,

(21)

Algorithm and Flowchart for all

Modules

◼ Control Module

◼ Init Module

◼ Read Module

◼ Calculate Module

(22)

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

(23)
(24)

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.

(25)

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.

(26)

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.

(27)

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

(28)

Binary Search

◼ The above procedure is repeated till the

desired value is found.

(29)

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.

(30)

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 )

(31)

Grades and Frequency distribution

Element Grades

(32)

Algorithms and Flowchart for

Frequency Distribution

◼ Control Module

◼ Init and Read Module

(33)

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

(34)

Contd…

◼ Frequency distribution shows the number of people who gave the same answer to one

question.

◼ 1(Yes) 2(no)

32 25

51 42

(35)

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

(36)

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

(37)

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

(38)

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

(39)

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.

(40)

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

(41)

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),

(42)

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.

(43)

Interactivity chart

Absent

Init Enter Calc Print

(44)

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

(45)

Algorithms and Flowcharts

◼ Control Module

◼ Init Module

◼ Menu Module

◼ Enter Module

◼ Calc Module

(46)

Array Order Reversal

◼ Problem: Rearrange the elements in an array so that they appear in reverse order.

(47)

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

(48)

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

Print

(49)

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]

(50)

Finding maximum number in an

Array

◼ Problem: Find the maximum number in a set of n numbers.

(51)

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];

(52)

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

References

Related documents

Java and is used to set a specified value to a specified index of a given object array.. Assign values project with

• In two-dimensional convolution, we replace each value in a two-dimensional array with a weighted average of the values surrounding it in

Each array has associated with it a width, height (two- and three-dimensional, rectangular, one-dimensional array, and cube map only), and depth (three-dimensional and

The array declaration in both the array i.e.in single dimensional array single subscript is used and in two dimensional array two subscripts are is used.. Its

Setting values in python line to declare one dimensional array declaration itself and how this gives us.. While creating list comprehension syntax, python two dimensional

 just as with a one-dimensional array, when a two- (or higher) dimensional array is passed as an argument, the base address of the caller’s array is sent to the function.  the

• Use the housekeeping genes to estimate  mean and stdev 

4.8 (continued) (f) Enqueuing number 6 to a queue storing 2, 4, and 8; (d–e) the same queue seen as a one-dimensional array with the last element (d) at the end of the array and (e)