1 arrays chapter 13 especially read 13.1 – 13.2 text introduces vectors, which we will not cover,...
TRANSCRIPT
1
ArraysChapter 13
Especially read 13.1 – 13.2
Text introduces vectors, which we will not cover, in 13.3
2
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
4
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
5
Arrays – Analogy to Locker Numbers in a Gym
0 1 2 3
Lockers
Locker NumberStuff that’s in the locker
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”
6
Arrays – Referring to a particular “locker”
The name of the Array as a
whole
This cell is referred to as
lockers[0]
0 1 2 3
Lockers
7
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
8
Declaring an Array of Integers
Declaring an arraytype name-of-array[size-of-array];
Example int myarray[10];
myarray 0 1 2 3 4 5 6 7 8 9
myarray holds 10 integers:
myarray[0] through myarray[9]
9
Defining an array of char
Defining an arraytype name-of-array[size-of-array];
Example char word[6];
word 0 1 2 3 4 5
word holds 6 char data:
word[0] through word[5]
Trivia: this is what
Is called a C-string!
10
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
11
Array storage and retrieval#include <iostream.h>void main(){float a[3]; a[0] = 55.55;a[1] = 11.11;a[2] = 33.33;cout << “a[0] = “<< a[0] << endl;cout << “a[1] = “<< a[1] << endl;cout << “a[2] = “<< a[2] << endl;
}
55.55
11.11
33.33
0
1
2
a
Put a 55 in first cell
Program Output:
a[0] =55.55
a[1] =11.11
a[2] =33.33Output last cell to console
12
More Array Storage and Retrieval
Each cell of the array is just like a separate variablecin>>a[2]; // input cell 2 of array acout<<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 cell1a[0]=a[1]+a[2]; //put sum of 1 & 2 in 0
File input and output is just as easyinfile>>a[0]>>a[1]>>a[2];
13
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
14
Arrays – Initialization List
When arrays are declared, they contain garbage data
Arrays can be initialized similar to structsfloat a[3] = {22.2, 44.4, 66.6};
22.2
44.4
66.6
0
1
2
a
ONLY AT DECLARATION TIME!
15
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”;
wordH 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
Terminator symbol
16
Arrays – Partial InitializingWhen 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
17
Your Turn, IDraw the following arrays
float data[8]={6.2, 3.4, 3.5};
char text[10]=“Albatross”;
int temp[6]={3};
18
Your Turn, IIDraw 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’;
19
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
20
Arrays – Using for loopsFor loops are a more compact way of processing loopsInstead of this:
cin>>a[0];cin>>a[1];cin>>a[2];cin>>a[3];cin>>a[4];cin>>a[5];
You can do this:
for (k=0; k<6; k++)
cin>>a[k];
21
Arrays – Using for loopsAnytime you see a pattern in what you are doing, you can replace it with a for-loop:
Instead of this:
a[0]=a[9];
a[1]=a[8];
a[2]=a[7];
a[3]=a[6];
a[4]=a[5];
You can do this:
for (k=0; k<5; k++)
a[k] = a[9-k];
22
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];
23
Your Turn IIIDraw 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;
24
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
25
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 run-time (not compiler!) error
26
Arrays – Index out of bounds
#include <iostream.h>
void main()
{
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!
Slots 4-6 are out of bounds
27
Arrays – Index out of bounds
This would display the entries of the arrayThe last three element displayed are junk since this is not part of the array and is part of memory which are not initializedSometimes you can accidentally modify data that does not belong to your array! Like…another variable!!
28
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
29
Passing an Array to a FunctionWhen 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[]
We don’t want to specify the size so function can work with different sized arrays. Size comes in as a second parameter
This would be a parameter in fn header
30
Passing an Array to a Function#include <iostream.h>
void Display(int data[], int N){ int k;cout<<“Array contains”<<endl;for (k=0; k<N; k++)
cout<<data[k]<<“ “;cout<<endl;
}void main() {int a[4] = { 11, 33, 55, 77 }; Display(a, 4);
}
An int array parameter
of unknown size
The size of the array
The array argument, no []
31
Passing an Array to a Function#include <iostream.h>int sum(int data[], int n); //PROTOTYPE
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
The array argument, no []
32
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;
}
33
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: