Lec13Arrays.ppt

advertisement
Arrays
Chapter 13
Especially read 13.1 – 13.2
Text introduces vectors, which
we will not cover, in 13.3
1
Agenda
Arrays – What are they ? 
Declaring Arrays
Array storage and retrieval
Initializing an array
Using loops to process an array
Array index out of bounds
Passing an array to a function
2
Arrays – What are they ?
A sequence of objects which
have the same type
3
Arrays – Think of Lockers
Much like a locker room with lockers of
the same size
Each locker is identified by a unique
number or an index
4
Arrays – Analogy to
Locker Numbers in a Gym
Locker Number
0
1
Stuff that’s in the locker
2
3
Lockers
In most programming languages including
C, C++ and Java, the first array index would
start with 0 and not 1. This is known as
zero-based indexing.
The Locker Number is also called the “Index” or “Position”
5
Arrays – Referring to a particular
“locker”
The name of
the Array as a
whole
0
1
2
3
Lockers
This cell is
referred to as
lockers[0]
6
Agenda
Arrays – What are they ?
Declaring Arrays 
Array storage and retrieval
Initializing an array
Using loops to process an array
Array index out of bounds
Passing an array to a function
7
Declaring an Array of Integers
Declaring an array
type name-of-array[size-of-array];
int myarray[10];
Example
myarray 0
1
2
3
4
5
6
7
8
9
myarray holds 10 integers:
myarray[0] through myarray[9]
8
Defining an array of char
Defining an array
type name-of-array[size-of-array];
char word[6];
Example
word
0
1
2
3
4
5
Trivia: this is what
Is called a C-string!
word holds 6 char data:
word[0] through word[5]
9
Agenda
Arrays – What are they ?
Declaring Arrays
Array storage and retrieval 
Initializing an array
Using loops to process an array
Array index out of bounds
Passing an array to a function
10
Array storage and retrieval
#include <iostream.h>
Put a 55 in first cell
void main()
{
a
float a[3];
0 55.55
a[0] = 55.55;
1
11.11
a[1] = 11.11;
a[2] = 33.33;
2 33.33
cout << “a[0] = “<< a[0] << endl;
cout << “a[1] = “<< a[1] << endl;
cout << “a[2] = “<< a[2] << endl;
Program Output:
}
Output last cell to console
a[0] =55.55
a[1] =11.11
a[2] =33.3311
More Array Storage and Retrieval
Each cell of the array is just like a separate variable
cin>>a[2]; // input cell 2 of array a
cout<<a[1];// display cell 1 of array a
cout<<a[0]<<“ “<<a[1]<<“ “<<a[2]<<endl;
a[1]=a[2]; //assign cell2 of a to cell1
a[0]=a[1]+a[2]; //put sum of 1 & 2 in 0
File input and output is just as easy
infile>>a[0]>>a[1]>>a[2];
12
Agenda
Arrays – What are they ?
Declaring Arrays
Array storage and retrieval
Initializing an array 
Using loops to process an array
Array index out of bounds
Passing an array to a function
13
Arrays – Initialization List
When arrays are declared, they contain
garbage data
Arrays can be initialized similar to structs
float a[3] = {22.2, 44.4, 66.6};
a
0
1
22.2
44.4
2
66.6
ONLY AT DECLARATION TIME!
14
Arrays – Initialization List
Arrays of char (C-strings) can be initialized
one of two ways:
char word[6] = {‘H’,’e’,’l’,’l’,’o’};
char word[6] = “Hello”;
Terminator symbol
word
H
e
l
l
o
\0
Method 2 puts a string terminator symbol (\0) in the array
You should make size one larger than number of letters
15
Arrays – Partial Initializing
When the list is smaller than the size of the
array, 0’s are used to fill in remaining cells
float a[6] = { 22.2, 44.4, 66.6};
a
22.2
44.4
66.6
0
0
0
16
Your Turn, I
Draw the following arrays
float data[8]={6.2, 3.4, 3.5};
char text[10]=“Albatross”;
int temp[6]={3};
17
Your Turn, II
Draw the following arrays after modifying
as shown:
float data[8]={6.2, 3.4, 3.5};
data[6]=data[2];
data[5]=data[2]+data[1];
char text[10]=“Albatross”;
text[1]=text[2]=‘x’;
18
Agenda
Arrays – What are they ?
Declaring Arrays
Array storage and retrieval
Initializing an array
Using loops to process an array 
Array index out of bounds
Passing an array to a function
19
Arrays – Using for loops
For loops are a more compact way of
processing loops
Instead of this:
You can do this:
cin>>a[0];
for (k=0; k<6; k++)
cin>>a[1];
cin>>a[k];
cin>>a[2];
cin>>a[3];
cin>>a[4];
cin>>a[5];
20
Arrays – Using for loops
Anytime you see a pattern in what you are
doing, you can replace it with a for-loop:
Instead of this:
You can do this:
a[0]=a[9];
for (k=0; k<5; k++)
a[1]=a[8];
a[k] = a[9-k];
a[2]=a[7];
a[3]=a[6];
a[4]=a[5];
21
Advantage of for loops
More compact, Saves typing
Easier to modify if size of array changes
Since for loops are so common, a technique
of using a const int for size of the array:
const int SIZE=10;
int k, data[SIZE];
for (k=0; k<SIZE; k++)
cin>>data[k];
22
Your Turn III
Draw the following arrays
float k, data[8];
for (k=0; k<8; k++) data[k]=k;
float k, data[8];
for (k=0; k<8; k++) data[k]=k-5;
float k, data[8];
for (k=0; k<8; k++) data[k]=7-k;
23
Agenda
Arrays – What are they ?
Declaring Arrays
Array storage and retrieval
Initializing an array
Using loops to process an array
Array index out of bounds 
Passing an array to a function
24
Arrays – NOTE
Initializing an array with more values
than the size of the array would lead to
a run-time (not compiler!) error
Referring to an array index larger than
the size of the array would lead to a runtime (not compiler!) error
25
Arrays – Index out of bounds
#include <iostream.h>
void main()
Slots 4-6 are out of bounds
{
const int SIZE=4;
float a[SIZE] = { 33.3, 44.4, 55.5, 66.6 };
for (int i=0; i < 7; i++)
cout << "a[" << i << "] = " << a[i] << endl;
}
You can get very weird results!
26
Arrays – Index out of bounds
This would display the entries of the
array
The last three element displayed are
junk since this is not part of the array
and is part of memory which are not
initialized
Sometimes you can accidentally modify
data that does not belong to your array!
Like…another variable!!
27
Agenda
Arrays – What are they ?
Declaring Arrays
Array storage and retrieval
Initializing an array
Using loops to process an array
Array index out of bounds
Passing an array to a function 
28
Passing an Array to a Function
When passing an array to a function, we
need to tell the compiler what the type
of the array is and give it a variable
name, similar to an array declaration
float a[]
This would be a parameter in fn header
We don’t want to specify the size so
function can work with different sized
arrays. Size comes in as a second
parameter
29
Passing an Array to a Function
#include <iostream.h> An int array parameter
of unknown size
void Display(int data[], int N)
{ int k;
cout<<“Array contains”<<endl;
for (k=0; k<N; k++)
cout<<data[k]<<“ “;
cout<<endl;
The size of the array
}
void main()
{
int a[4] = { 11, 33, 55, 77 };
Display(a, 4);
}
The array argument,
30 no []
Passing
an
Array
to
a
Function
#include <iostream.h>
int sum(int data[], int n);
//PROTOTYPE
The array argument, no
void main()
{
int a[] = { 11, 33, 55, 77 };
int size = sizeof(a)/sizeof(int);
cout << "sum(a,size) = " << sum(a,size) << endl;
}
int sum(int data[], int n)
{
int sum=0;
for (int i=0; i<n;i++)
sum += data[i];
return sum;
}
[]
An int array parameter
of unknown size
The size of the array
31
Arrays are always Pass By Reference
Arrays are automatically passed by
reference. Do not use &.
If the function modifies the array, it is also
modified in the calling environment
void Zero(int arr[], int N)
{
for (int k=0; k<N; k++) arr[k]=0;
}
32
Your Turn
Add function calls to Zero out the array
below, and Display it:
int samples[10000];
// your call to Zero here:
// your call to Display here:
33
That’s a wrap !
What we learned today:
What are arrays
Array indexing
Array initialization
Array index out of bound error
Passing arrays to functions
34
Download