8-7 two-dimensional arrays

38
Computer Science: A Structured Programming Approach Using C 1 -7 Two-Dimensional Arrays The arrays we have discussed so far are known The arrays we have discussed so far are known as one-dimensional arrays because the data as one-dimensional arrays because the data are organized linearly in only one direction. are organized linearly in only one direction. Many applications require that data be stored Many applications require that data be stored in more than one dimension. One common in more than one dimension. One common example is a table, which is an array that example is a table, which is an array that consists of rows and columns. consists of rows and columns. Declaration Passing A Two-Dimensional Array Topics discussed in this section: Topics discussed in this section:

Upload: madeline-boone

Post on 01-Jan-2016

42 views

Category:

Documents


1 download

DESCRIPTION

8-7 Two-Dimensional Arrays. - PowerPoint PPT Presentation

TRANSCRIPT

Computer Science: A Structured Programming Approach Using C 1

8-7 Two-Dimensional Arrays

The arrays we have discussed so far are known as one-The arrays we have discussed so far are known as one-dimensional arrays because the data are organized dimensional arrays because the data are organized linearly in only one direction. Many applications linearly in only one direction. Many applications require that data be stored in more than one require that data be stored in more than one dimension. One common example is a table, which is dimension. One common example is a table, which is an array that consists of rows and columns.an array that consists of rows and columns.

DeclarationPassing A Two-Dimensional Array

Topics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 2

FIGURE 8-34 Two-dimensional Array

Computer Science: A Structured Programming Approach Using C 3

FIGURE 8-35 Array Of Arrays

Computer Science: A Structured Programming Approach Using C 4

PROGRAM 8-15 Fill Two-dimensional Array

Computer Science: A Structured Programming Approach Using C 5

FIGURE 8-36 Memory Layout

Computer Science: A Structured Programming Approach Using C 6

PROGRAM 8-16 Convert Table to One-dimensional Array

Computer Science: A Structured Programming Approach Using C 7

PROGRAM 8-16 Convert Table to One-dimensional Array

Computer Science: A Structured Programming Approach Using C 8

FIGURE 8-37 Passing a Row

Computer Science: A Structured Programming Approach Using C 9

FIGURE 8-38 Calculate Average of Integers in Array

Computer Science: A Structured Programming Approach Using C 10

FIGURE 8-39 Example of Filled Matrix

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 8-17 Fill Matrix

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 8-17 Fill Matrix

Computer Science: A Structured Programming Approach Using C 13

8-8 Multidimensional Arrays

Multidimensional arrays can have three, four, or more Multidimensional arrays can have three, four, or more dimensions. The first dimension is called a plane, dimensions. The first dimension is called a plane, which consists of rows and columns. The C language which consists of rows and columns. The C language considers the three-dimensional array to be an array considers the three-dimensional array to be an array of two-dimensional arrays.of two-dimensional arrays.

Declaring Multidimensional ArraysTopics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 14

FIGURE 8-40 A Three-dimensional Array (3 x 5 x 4)

Computer Science: A Structured Programming Approach Using C 15

FIGURE 8-41 C View of Three-dimensional Array

Computer Science: A Structured Programming Approach Using C 16

8-9 Programming Example— Calculate Averages

We previously introduced the programming concept We previously introduced the programming concept known as incremental development. In this chapter we known as incremental development. In this chapter we develop an example—calculate average—that contains develop an example—calculate average—that contains many of the programming techniques. many of the programming techniques.

First Increment: mainYour First C Second Increment: Get DataThird Increment: Calculate Row AveragesFourth Increment: Calculate Column AveragesFifth Increment: Print Tables

Topics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 17

FIGURE 8-42 Data Structures For Calculate Row–Column Averages

Computer Science: A Structured Programming Approach Using C 18

FIGURE 8-43 Calculate Row–Column Average Design

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 8-18 Calculate Row and Column Averages: main

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 8-19 Calculate Row and Column Averages: Get Data

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 8-19 Calculate Row and Column Averages: Get Data

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 8-19 Calculate Row and Column Averages: Get Data

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 8-19 Calculate Row and Column Averages: Get Data

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

Computer Science: A Structured Programming Approach Using C 29

8-10 Software Engineering

In this section, we discuss two basic concepts: testing In this section, we discuss two basic concepts: testing and algorithm efficiency. To be effective, testing must and algorithm efficiency. To be effective, testing must be clearly thought out. We provide some concepts for be clearly thought out. We provide some concepts for testing array algorithms by studying sorting and testing array algorithms by studying sorting and searching. We then continue the algorithm efficiency searching. We then continue the algorithm efficiency discussion started in Chapter 6.discussion started in Chapter 6.

Testing SortsTesting SearchesAnalyzing Sort AlgorithmsAnalyzing Search Algorithms

Topics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 30

Table 8-2 Recommended Sort Test Cases

Computer Science: A Structured Programming Approach Using C 31

When testing an array search, always access the first and last elements.

NoteNote

Computer Science: A Structured Programming Approach Using C 32

Table 8-3 Test cases for searches

Computer Science: A Structured Programming Approach Using C 33

The efficiency of the bubble sort is O(n2).

NoteNote

Computer Science: A Structured Programming Approach Using C 34

The efficiency of the selection sort is O(n2).

NoteNote

Computer Science: A Structured Programming Approach Using C 35

The efficiency of the insertion sort is O(n2).

NoteNote

Computer Science: A Structured Programming Approach Using C 36

The efficiency of the sequential search is O(n).

NoteNote

Computer Science: A Structured Programming Approach Using C 37

The efficiency of the binary search is O(logn).

NoteNote

Computer Science: A Structured Programming Approach Using C 38

Table 8-4 Comparison of binary and sequential searches