1 objectives ❏ to understand the basic concepts and uses of arrays ❏ to be able to define c...
TRANSCRIPT
1
Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions ❏ To understand the classical approach to sorting arrays: bubble sorting ❏ Array Application ❏ Two-Dimensional Arrays ❏ Multidimensional Arrays
Chapter 8Chapter 8 ArraysArrays
3
8-1 Concepts
Imagine we have a problem that requires us to read, Imagine we have a problem that requires us to read, process, and print a large number of integers. We process, and print a large number of integers. We must also keep the integers in memory for the duration must also keep the integers in memory for the duration of the program.of the program. To process large amounts of data we need a To process large amounts of data we need a powerful data structure, the array. An powerful data structure, the array. An arrayarray is a is a collection of elements of the same data type.collection of elements of the same data type. Since an array is a sequenced collection, we can Since an array is a sequenced collection, we can refer to the elements in the array as the first element, refer to the elements in the array as the first element, the second element, and so forth until we get to the last the second element, and so forth until we get to the last element.element.
7
8-2 Using Arrays in C
In this section, we first show how to In this section, we first show how to declaredeclare and and definedefine arrays. Then we present several typical applications arrays. Then we present several typical applications using arrays including using arrays including readingreading values into arrays, values into arrays, accessingaccessing and and exchanging elements exchanging elements in arrays, and in arrays, and printing arrays. printing arrays.
Declaration and DefinitionAccessing Elements in ArraysStoring Values in ArraysIndex Range Checking
Topics discussed in this section:Topics discussed in this section:
10
FIGURE 8-8 Initializing Arrays
Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized
by inputting or assigning the values.NoteNote
11
FIGURE 8-9 Exchanging Scores—the Wrong Way
One array cannot be copied to another using assignment.
NoteNote
16
8-3 Inter-function Communication
To process arrays in a large program, we have to be To process arrays in a large program, we have to be able to pass them to functions. We can pass arrays in able to pass them to functions. We can pass arrays in two ways: pass individual elements or pass the whole two ways: pass individual elements or pass the whole array. In this section we discuss first how to pass array. In this section we discuss first how to pass individual elements and then how to pass the whole individual elements and then how to pass the whole array.array.
Passing Individual ElementsPassing the Whole Array
Topics discussed in this section:Topics discussed in this section:
22
8-4 Array Applications
In this section we study two array applications: In this section we study two array applications: frequency arrays with their graphical representations frequency arrays with their graphical representations and random number permutations.and random number permutations.
Frequency ArraysHistogramsRandom Number Permutations
Topics discussed in this section:Topics discussed in this section:
35
8-5 Sorting
One of the most common applications in computer One of the most common applications in computer science is sorting—the process through which data are science is sorting—the process through which data are arranged according to their values. We are arranged according to their values. We are surrounded by data. If the data are not ordered, we surrounded by data. If the data are not ordered, we would spend hours trying to find a single piece of would spend hours trying to find a single piece of information. information.
Bubble SortTopics discussed in this section:Topics discussed in this section:
36
PROGRAM 8-10
Bubble Sort
/* ==================== bubble Sort ====================Sort list using bubble sort. Adjacent elements are compared and exchanged until list is ordered. Pre the list must contain at least one item last contains index to last element in list. Post list rearranged in sequence low to high */
int main (){ int temp; int i, j; int list[10]={1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
printf("Before sorting\n"); for(i = 0; i < 10; i++) printf("Data %d: %d\n", i, list[i]); // Outer loop for (i = 0; i < 10-1; i++) { // Inner loop: Bubble up one element each pass for (j = i+1 ; j < 10; j++) if (list[i] > list[j]) { temp = list[i]; list[i]=list[j]; list[j] = temp; } // if } // for i printf("After sorting\n"); for(i = 0; i < 10; i++) printf("Data %d: %d\n", i, list[i]); system("pause"); return 0;}
37
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. linearly in only one direction. Many applications require that data be stored in more Many applications require that data be stored in more than one dimension. One common example is a table, than one dimension. One common example is a table, which is an array that consists of rows and columns.which is an array that consists of rows and columns.
DeclarationPassing A Two-Dimensional Array
Topics discussed in this section:Topics discussed in this section:
45
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:
47
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: