Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.

Similar presentations


Presentation on theme: "1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3."— Presentation transcript:

1 1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3

2 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

3 3 Arrays – What are they ? A sequence of objects which have the same type

4 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 5 Arrays – Analogy to Locker Numbers in a Gym 0123 Lockers Locker Number Stuff 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 6 Arrays – Referring to a particular “locker” The name of the Array as a whole This cell is referred to as lockers[0] 0123 Lockers

7 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 8 Declaring an Array of Integers Declaring an array type name-of-array[size-of-array]; Example int myarray[10]; myarray 0123456789 myarray holds 10 integers: myarray[0] through myarray[9]

9 9 Defining an array of char Defining an array type name-of-array[size-of-array]; Example char word[6]; word 012345 word holds 6 char data: word[0] through word [5] Trivia: this is what Is called a C-string!

10 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 11 Array storage and retrieval #include 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.33 Output last cell to console

12 12 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];

13 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 14 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}; 22.2 44.4 66.6 0 1 2 a ONLY AT DECLARATION TIME!

15 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”; word Hello\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 16 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.244.466.6000

17 17 Your Turn, I Draw the following arrays float data[8]={6.2, 3.4, 3.5}; char text[10]=“Albatross”; int temp[6]={3};

18 18 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’;

19 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 20 Arrays – Using for loops For loops are a more compact way of processing loops Instead 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 21 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: 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 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 >data[k];

23 23 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;

24 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 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 26 Arrays – Index out of bounds #include 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 27 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!!

28 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 29 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[] 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 30 Passing an Array to a Function #include 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 31 Passing an Array to a Function #include 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 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 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:

34 34 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


Download ppt "1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3."

Similar presentations


Ads by Google