[go: up one dir, main page]

0% found this document useful (0 votes)
4 views22 pages

Slide - 6 (Array - 1)

The document provides an introduction to arrays, explaining their structure as collections of variable data that share the same name and type, and their uses in managing lists of similar items. It covers array declaration, accessing elements, pitfalls, and the use of loops for traversal, as well as initializing and working with multidimensional arrays. Key concepts include the importance of maintaining array bounds and using defined constants for array sizes to enhance code readability and maintainability.

Uploaded by

vectormr1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Slide - 6 (Array - 1)

The document provides an introduction to arrays, explaining their structure as collections of variable data that share the same name and type, and their uses in managing lists of similar items. It covers array declaration, accessing elements, pitfalls, and the use of loops for traversal, as well as initializing and working with multidimensional arrays. Key concepts include the importance of maintaining array bounds and using defined constants for array sizes to enhance code readability and maintainability.

Uploaded by

vectormr1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Arrays

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
Initializing Arrays
 Arrays can be initialized at declaration

 Size cannot be variable or named constant


 Equivalent to

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

 Performs initialization

16
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

17
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

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

19
Three-dimensional Visualization

20
Multidimensional Array Parameters
 Must specify size after first dimension

21
THE END

22

You might also like