CH 06 - A Arrays

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Programming in C

1
Introduction to Arrays
 A collection of variable data
 Same name
 Same type
 Contiguous block of memory
 Can manipulate or use
 Individual variables or
 ‘List’ as one entity

Celsius
temperatures:
I’ll name it c.
Type is int.
2
Introduction to Arrays
 Used for lists of like items
 Scores, speeds, weights, etc.
 Avoids declaring multiple simple variables
 Used when we need to keep lots of values in memory
 Sorting
 Determining the number of scores above/below the
mean
 Printing values in the reverse order of reading
 Etc.

3
Declaring Arrays
 General Format for declaring arrays
<data type> <variable> [<size>];
 Declaration
 Declaring the array  allocates memory
 Static entity - same size throughout program
 Examples
Type is int.
Name is c.

4
Defined Constant as Array Size
 Use defined/named constant for array size
 Improves readability
 Improves versatility
 Improves maintainability
 Examples:

5
Powerful Storage Mechanism
 Can perform subtasks like:
 "Do this to i-th indexed variable"
where i is computed by program
 "Fill elements of array scores from user input"
 "Display all elements of array scores“
 “Sort array scores in order”
 “Determine the sum or average score”
 "Find highest value in array scores"
 "Find lowest value in array scores"

6
Accessing Array Elements
 Individual parts called many things:
 Elements of the array
 Indexed or
subscripted variables
 To refer to an element:
 Array name and subscript or index
 Format: arrayname[subscript]
 Zero based
 c[0] refers to c0, c sub zero,
the first element of array c

7
Accessing Array Elements
 Example

 Note two uses of brackets:


 In declaration, specifies SIZE of array
 Anywhere else, specifies a subscript/index

8
Accessing Array Elements
 Example
 Given the declaration

 We reference elements of scores by

scores [0]

scores [1]

… subscript/index

scores [11]

9
Accessing Array Elements
 Size, subscript need not be literal constant
 Can be named constant or expression

10
Major Array Pitfall
 Array indexes go from 0 through size-1!
 C will 'let' you go out of the array’s bounds
 Unpredictable results – may get segmentation fault
 Compiler will not detect these errors!
 Up to programmer to 'stay in bounds‘
?

11
for-loops with Arrays
 Natural counting loop
 Naturally works well 'counting thru' elements
of an array
 General form for forward direction
 for (subscript = 0; subscript < size; subscript++)
 General form for reverse direction
 for (subscript = size-1; subscript >= 0; subscript--)

12
for-loops with Arrays Examples

Score 1 is 56 Score 12 is 87
Score 2 is 52 Score 11 is 97
Score 3 is 80 Score 10 is 86
Score 4 is 74 Score 9 is 80
... ...
Score 12 is 87 Score 1 is 56

13
Uses of Defined Constant
 Use everywhere size of array is needed
 In for-loop for traversal:

 In calculations involving size:

 When passing array a function:

14
Array as Function Parameter
 Include type and brackets []
 Size inside brackets is optional and is ignored
 Passes pointer/reference to array
 Function can modify array elements
 Common to also pass size
 Example:

15
Initializing Arrays
 Arrays can be initialized at declaration

 Size cannot be variable or named constant


 Equivalent to

16
Auto-Initializing Arrays
 If fewer values than size supplied:
 Fills from beginning
 Fills 'rest' with zero of array base type
 Declaration

 Performs initialization

17
Auto-Initializing Arrays
 If array size is left out
 Declares array with size required based on number of
initialization values
 Example:

 Allocates array scores with size of 3

18
Multidimensional Arrays
 Arrays with more than one dimension
 Declaration: Additional sizes each enclosed in brackets
 Two dimensions
 Table or ‘array of arrays’

 Requires two subscripts – row and column

19
Initializing
Multidimensional
 Nested lists
 Unspecified values set to zero
 2D Example:

20
Three-dimensional Visualization

21
Multidimensional Array Parameters
 Must specify size after first dimension

22
Programming in C

THE END

23

You might also like