Академический Документы
Профессиональный Документы
Культура Документы
Overview
• To solve many programming problems, it is more
efficient to group data items together in main memory
than to allocate an individual memory cell for each
variable.
• C allows a programmer to group such related data items
together into a single composite data structure.
• In this chapter, we look at one such data structure: the
array, which is a collection of data items of the same
type.
INTRODUCTION TO ARRAYS, SEQUENTIAL
ACCESS, AND ARRAY ARGUMENTS
(CHAPTER 7.1-7.5)
Declaring and Referencing Arrays
• An array is a collection of two or more adjacent memory cells,
called array elements.
• Declaration
double x[8];
– instructs the compiler to associate eight memory cells with
the name x
– these memory cells will be adjacent to each other in
memory
– each element of array x may contain a single type double
value
– so, eight double number may be stored and referenced
using the array name x
Declaring and Referencing Arrays
• To process the data stored in an array, we reference each
individual element by specifying the array name and
identifying the element desired (e.g. element 3 of array x).
• Subscripted variable
x[n];
may be used to reference the n-th element of array x.
• Subscripted variable
x[n];
may be used to reference the n-th element of array x.
printf(“%.lf”, x[0]);
x[3] = 25.0;
sum = x[0] + x[1];
sum += x[2];
x[3] += 1.0;
x[2] = x[0] + x[1];
Example 7.1
double x[8];
Array x
printf(“%.lf”, x[0]);
x[3] = 25.0; // Stores the value 25.0 in x[3]
sum = x[0] + x[1];
sum += x[2];
x[3] += 1.0;
x[2] = x[0] + x[1];
Example 7.1
double x[8];
Array x
printf(“%.lf”, x[0]);
x[3] = 25.0;
sum = x[0] + x[1]; // Stores x[0] + x[1], 28.0, in variable sum
sum += x[2];
x[3] += 1.0;
x[2] = x[0] + x[1];
Example 7.1
double x[8];
Array x
printf(“%.lf”, x[0]);
x[3] = 25.0;
sum = x[0] + x[1];
sum += x[2]; // Adds x[2] to sum, 28.0 + 6.0 = 34.0
x[3] += 1.0;
x[2] = x[0] + x[1];
Example 7.1
double x[8];
Array x
printf(“%.lf”, x[0]);
x[3] = 25.0;
sum = x[0] + x[1];
sum += x[2];
x[3] += 1.0; // Adds 1.0 to x[3], 25.0 + 1.0 = 26.0
x[2] = x[0] + x[1];
Example 7.1
double x[8];
Array x
printf(“%.lf”, x[0]);
x[3] = 25.0;
sum = x[0] + x[1];
sum += x[2];
x[3] += 1.0;
x[2] = x[0] + x[1]; // Stores x[0] + x[1], 16.0 + 12.0 = 28.0 in
x[2]
Array Initialization
• In the following statement, we initialize a 25-element
array with the prime numbers less than 100.
int prime_lt_100[] = {2, 3, 5, 7, 11, 13, 17,
19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97};
Statement Explanation
i = 5;
printf(“%d %.lf”, 4, x[4]); Displays 4 and 2.5 (value of x[4])
printf(“%.lf”, x[i] + 1) Display 13.0 (value of x[5] plus 1)
printf(“%.lf”, x[i +1]) Display 14.0 (value of x[6])
printf(“%.lf”, x[i] + i) Display 17.0 (value of x[5] plus 5)
printf(“%.lf”, x[2*i - 3]) Display -54.5 (value of x[7])
Array Subscripts
• Example: Array x
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
i = 5;
printf(“%d %.lf”, x[(int)x[4]]); Displays 6.0 (value of x[2])
printf(“%.lf”, x[i++]) Display 12.0 (value of x[5]) then
assign 6 to i
printf(“%.lf”, x[--i]) Assign 5 (6-1) to i and then display
12.0 (value of x[5])
Using for Loops for Sequential Access
• In C, we can process the elements of an array in
sequence using an indexed for loop, a counting loop
whose loop control variable runs from 0 to one less
than the array size.
• Example: (assume SIZE has been defined to be 11)
int square[SIZE], i;
for (i=0; i < SIZE; i++)
square[i] = i * i;
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
0 1 4 9 16 25 36 49 64 81 100
Using for Loops for Sequential Access
• Statistical Computations Using Arrays
One common use of arrays is for storage of a
collection of related data values, which are used to
perform some simple statistical computations.
Example:
sum = 0;
for (i = 0; i < MAX_ITEM; i++){
sum += x[i];
}
Using for Loops for Sequential Access
sum_sqr = 0;
for (i = 0; i < MAX_ITEM; i++){
sum_sqr += x[i] * x[i];
}
• The call
scanf(“%lf”, &x[i]);
uses array element x[i] as an output argument to
function scanf.
Using Array Elements as Function
Arguments
• Example:
The function prototype below shows one type double input
parameter (arg_1) and two type double * output parameters
(arg2_p and arg3_p)
fill_array(x, 5, 1);
fill_array(&x[0], 5, 1);
Array Arguments
• Argument Correspondence for Array Parameters
In the declaration for function fill_array, we can use
either parameter declaration:
int list[]
int *list
Example:
If x is a five-element array type int values, statement
x_large = get_max(x,5)
causes function get_max to search array x for its largest
element; this value is returned and stored in x_large.
Array Arguments
• Returning an Array Result
In C, defining a function of the variety modeled in the figure
below requires use of an output parameter to send the result
array back to the calling module.
i=0;
while (!found && i < n){
if (arr[i] == target)
found = 1;
else
++i;
}
if (found)
where = i;
else
where = NOT_FOUND;
return (where);
}
Sorting an Array
• Algorithm
o for each value of fill from 0 to n-2
✔ Find index_of_min, the index of the
smallest element in the unsorted
subarray list[fill] through list[n-1].
✔ If fill is not the position of the smallest
element (index_of_min)
❖ Exchange the smallest element with
the one at position fill.
Sorting an Array
• Trace of Selection Sort
fill is 0.
Sorting an Array
• Trace of Selection Sort
fill is 0.
Find the smallest element in subarray
list[1] through list[3] and swap it with
list[0].
Sorting an Array
• Trace of Selection Sort
fill is 0.
Find the smallest element in subarray
list[1] through list[3] and swap it with
list[0].
Sorting an Array
• Trace of Selection Sort
fill is 0.
Find the smallest element in subarray
list[1] through list[3] and swap it with
list[0].
Sorting an Array
• Trace of Selection Sort
fill is 1.
Find the smallest element in subarray
list[1] through list[3] and swap it with
list[1].
Sorting an Array
• Trace of Selection Sort
fill is 1.
Find the smallest element in subarray
list[1] through list[3] and swap it with
list[1].
No exchange needed.
Sorting an Array
• Trace of Selection Sort
fill is 2.
Find the smallest element in subarray
list[2] through list[3] and swap it with
list[2].
Sorting an Array
• Trace of Selection Sort
fill is 2.
Find the smallest element in subarray
list[2] through list[3] and swap it with
list[2].
Sorting an Array
• Trace of Selection Sort
fill is 2.
Find the smallest element in subarray
list[2] through list[3] and swap it with
list[2].
Sorting an Array
• Trace of Selection Sort
fill is 2.
Find the smallest element in subarray
list[2] through list[3] and swap it with
list[2].
Sorting an Array
• Trace of Selection Sort
if (fill != index_of_min){
temp = list[index_of_min];
list[index_of_min] = list[fill];
list[fill] = temp;
}
}
}
PARALLEL ARRAYS, ENUMERATED
TYPES, AND MULTIDIMENSIONAL
ARRAYS (CHAPTER 7.7 & 7.8)
Parallel Arrays and Enumerated Types
• Parallel arrays are two or more arrays with
the same number of elements used for storing
related information about a collection of data
objects.
• Enumerated type is a data type whose list of
values is specified by the programmer in a
type declaration.
Multidimensional Arrays
• Multidimensional array is an array with two or more
dimensions.
• We will use two-dimensional arrays to represent
tables of data, matrices, and other two-dimensional
objects.
• A two-dimensional object that many are familiar
with is a tic-tac-toe board. The array declaration
char tictac[3][3];
allocates storage for a two-dimensional array
(tictac) with three rows and three columns.