Arrays - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Arrays

Description:

... specify that the argument is an array by placing brackets after the name. Do NOT put the size of the array inside the brackets. Example: void printArray ( int arr ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 16
Provided by: vana
Category:
Tags: arrays | brackets | the

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • In many cases we need a group of nearly identical
    variables.
  • Example make one variable for the grade of each
    student in the class
  • This results in a difficult to maintain and
    error-prone program.
  • Instead use arrays
  • Array a named location in memory for many
    values of the same type, stored sequentially and
    accessed through an index
  • Compare variable a named location in memory
    for one value.

2
Arrays
  • Think of an array a sequence of boxes, each one
    holding a value
  • Example An array named scores that holds the
    quiz scores of 5 students

element 0
element 1
element 2
element 3
element 4
11 10 12 9 11
scores
3
Arrays
  • Array declaration syntax
  • element_type array_name size
  • element_type is the type of the elements stored
    in the array.
  • array_name is the name of the array. Naming rules
    are the same as with variables.
  • size must be a constant (known at compile time)
  • no variables allowed
  • you cannot change the array size at runtime
  • use a define directive to define the size.

4
Arrays
  • Declaring and initializing an array
  • Example 1 int scores5
  • the elements of scores have not been initialized.
    They may be initialized one at a time (usually in
    a for loop
  • for (int i 0 i lt 5 i)
  • cout ltlt Enter score
  • cin gtgt scoresi
  • Example 2 int scores5 11, 10, 12, 9,
    11
  • the elements are initialized at declaration time.
    When this is done, the size of the array may be
    omitted from the declaration.

5
Arrays
  • Accessing the elements of an array.
  • To access the element at index i specify the name
    of the array and the index enclosed in brackets.
  • Example scores3 11
  • CAUTION!
  • Indexing always starts at zero and ends at size-1
  • int scores5
  • scores0 11
  • scores1 12
  • scores2 10
  • scores3 9
  • scores4 11

The compiler will NOT catch a wrong index. Typing
scores10 or scores-1 can corrupt your data.
6
Arrays
  • How exactly do they work?
  • int scores5 says I need a chunk of memory big
    enough to hold 5 integer variables. Thats a
    total of 54 20 bytes.
  • scores itself represents the memory address where
    this chunk of memory begins. Think of it as a
    special kind of constant that holds the address
    of a location in memory.
  • Example

The output of this program is 0xbfffb240 (the
address of arr in hexadecimal)
int main () int arr2 cout ltlt
arr ltlt endl return 0
7
Arrays
  • How exactly do they work?
  • scoresi represents the contents in box i
    starting at address scores. In other words, it
    represents the contents at address scores i
    sizeof(int)

8
Arrays functions
  • Each element of an array acts just like an
    ordinary variable
  • Like any ordinary variable, you can pass a single
    array element to a function as one of its
    arguments.

copy (pass by value)
int main () int nums3 3, 6, 5 int
sq sq square( nums2 ) cout ltlt nums2
ltlt squared is ltlt
sq ltlt endl return 0
int square( int x ) return x x
Output 5 squared is 25
9
Arrays functions
  • To pass the entire array as an argument
  • In the function prototype specify that the
    argument is an array by placing brackets after
    the name.
  • Do NOT put the size of the array inside the
    brackets.
  • Example
  • void printArray ( int arr )
  • Call the function using ONLY the array name as an
    argument.
  • Example
  • int nums5 1,2,3,4,5
  • printArray( nums )

10
Arrays functions
int main () int scores5 11,12, 10, 9,
11 printArray( scores, 5 ) return 0
void printArray(int arr, int size) for
(int i0 iltsize i) cout ltlt arri ltlt
cout ltlt endl
  • The parameters are again passed by value.
    However, the value of scores (which is copied
    onto arr) is the address where the array begins.
    As a result, both arr and scores refer to the
    same location in memory! This allows a function
    to modify the elements of an array that is passed
    as an argument.

11
Arrays functions
int main () int scores5 11,12, 10, 9,
11 cleanArray( scores, 5) printArray(
scores, 5 ) return 0
void printArray(int arr, int size) for
(int i0 iltsize i) cout ltlt arri ltlt
cout ltlt endl
void cleanArray(int arr, int size) for
(int i0 iltsize i) arri 0
This actually modifies the values stored in
scores.
Output 0 0 0 0 0
12
Multidimensional arrays
1 row of 5 elements
  • 1D array
  • 2D array
  • 3D array

3x5 array. Think of it as an array of arrays (
an array of three elements which are arrays of
five elements)
3x3x1 array.
13
Multidimensional arrays
  • How are they stored in memory?
  • What are the indices of the elements?

1
2
1
2
3
4
5
6
3
4
row 1 row 2 row 3
5
6
3x2 array
0,0
0,1
0,2
0,3
2x4 array
1,0
1,1
1,2
1,3
row
column
14
Multidimensional arrays
  • Declaration similar to 1D, but specify rows,
    columns
  • Example double stats104 // a 10x4 array
    of doubles
  • Initialization similar to 1D. Use nested
    for-loops or initialize at declaration.
  • Example 1
  • Example 2

for (int i 0 i lt 10 i) for (int j 0 j lt
4 j) stats i j 0.0
int grid32 0, 1 ,
1, 1 , 0, 0

row 1
row 2
row 3
15
Multidimensional arrays
  • CAUTION! When a multidimensional array is an
    argument to a function, the size of all but the
    first subscript MUST be specified.
  • Example
  • void initArray( int arr10, int numrows) //
    prototype
  • ...
  • int main()
  • int nums510
  • initArray(nums, 5) // function call
  • ...
Write a Comment
User Comments (0)
About PowerShow.com