Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements...
-
Upload
donna-clarke -
Category
Documents
-
view
255 -
download
0
description
Transcript of Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements...
![Page 1: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/1.jpg)
ArraysArrays
Chapter 12Chapter 12
![Page 2: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/2.jpg)
OverviewOverview
• Arrays and their properties• Creating arrays• Accessing array elements• Modifying array elements• Loops and arrays
![Page 3: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/3.jpg)
Arrays and Their PropertiesArrays and Their Properties
• Hold several values of the same type
• Based on a slot number (the index number)
• Instant access• Linear (one after the other)• Static – once their size is set, it’s
set…
![Page 4: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/4.jpg)
4
Arrays Hold Multiple Arrays Hold Multiple ValuesValues
Regular variable
Array days stores 3 values of type int
Variable count stores one value of type int
int days[ 3 ];
int count;
Array variable
count
Allocates memory for 3 ints
days1st element 2nd element 3rd element
5
2 3 4
![Page 5: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/5.jpg)
What arrays look likeWhat arrays look like
• Things to notice
• There are 7 slots, with index numbers 0 – 6
• The name of the array is myArray• Easy to be off by one
0 1 2 3 4 5 6myArray
![Page 6: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/6.jpg)
Creating ArraysCreating Arrays
<data type> <name> [<size>];
• Notice that we can create an array of any data type, just by changing the data type!
![Page 7: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/7.jpg)
ExamplesExamples
• An array of shorts:short someArray [50];
• An array of floats:float myArray [25];
• An array of booleans:bool list [65];
• An array of chars:char characters [255];
![Page 8: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/8.jpg)
Modifying an ArrayModifying an Array
• You must specify which slot you are putting information in
• Example:int myArray [50];myArray [ 3 ] = 12;
• This won’t work:int myArray [50];myArray = 12;
• Data type on the left of = is an array• Data type on right of = is an int
0 1 2 3 4…
49
12
![Page 9: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/9.jpg)
Accessing InformationAccessing Information
• Copying information out of a particular slot
int clientAge;clientAge = myArray [ 4 ];
• This copies information from the fifth slot (slot four) into the variable clientAge
![Page 10: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/10.jpg)
Initializing ArraysInitializing Arrays(large arrays)(large arrays)
• For most arrays, you will use a loop to initialize
• Example: Create an array of 5 bytes and fill each slot with the number 42
byte myList [5];for (int counter = 0; counter < 5; counter++) {
myList [counter] = 42;}
![Page 11: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/11.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}
![Page 12: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/12.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
0 0 0 0 0
![Page 13: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/13.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
0 0 0 0 0
0counter
![Page 14: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/14.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
0 0 0 0 0
0counter
![Page 15: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/15.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
0 0 0 0 0
0counter
![Page 16: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/16.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 0 0 0 0
0counter
![Page 17: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/17.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 0 0 0 0
1counter
![Page 18: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/18.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 0 0 0 0
1counter
![Page 19: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/19.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 0 0 0 0
1counter
![Page 20: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/20.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 0 0 0
1counter
![Page 21: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/21.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 0 0 0
2counter
![Page 22: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/22.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 0 0 0
2counter
![Page 23: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/23.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 0 0 0
2counter
![Page 24: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/24.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 0 0
2counter
![Page 25: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/25.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 0 0
3counter
![Page 26: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/26.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 0 0
3counter
![Page 27: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/27.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 0 0
3counter
![Page 28: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/28.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 0
3counter
![Page 29: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/29.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 0
4counter
![Page 30: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/30.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 0
4counter
![Page 31: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/31.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 0
4counter
![Page 32: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/32.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 42
4counter
![Page 33: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/33.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 42
5counter
![Page 34: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/34.jpg)
Line by LineLine by Line
byte myList [5];for (int counter = 0; counter < 5;
counter++) {myList [counter] = 42;
}0 1 2 3 4
42 42 42 42 42
5counter
false
![Page 35: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/35.jpg)
Finding the Smallest ElementFinding the Smallest Element
• If you were given an array of numbers, how would YOU do it?
• Computers can only compare two at a time
• Go through entire list• Keep track of smallest so far• Let’s assume
– We have an array of 5 ints– The array contains random unknown numbers– The name of the array is called randomArray
![Page 36: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/36.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4smallestSoFar
![Page 37: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/37.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4smallestSoFar
![Page 38: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/38.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 42smallestSoFar
![Page 39: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/39.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 42smallestSoFar
1counter
![Page 40: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/40.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 42smallestSoFar
1counter
![Page 41: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/41.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 42smallestSoFar
1counter
Is 42 > 17?
![Page 42: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/42.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
1counter
![Page 43: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/43.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
1counter
![Page 44: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/44.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
2counter
![Page 45: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/45.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
2counter
![Page 46: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/46.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
2counter
Is 17 > 42?
![Page 47: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/47.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
2counter
![Page 48: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/48.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
3counter
![Page 49: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/49.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
3counter
![Page 50: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/50.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 17smallestSoFar
3counter
Is 17 > -8?
![Page 51: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/51.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
3counter
![Page 52: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/52.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
3counter
![Page 53: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/53.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
4counter
![Page 54: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/54.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
4counter
![Page 55: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/55.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
4counter
Is -8 > 4?
![Page 56: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/56.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
4counter
![Page 57: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/57.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
5counter
![Page 58: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/58.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
5counter
![Page 59: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/59.jpg)
Another TraceAnother Trace
int smallestSoFar;smallestSoFar = randomArray[0];for (int counter = 1; counter < 5; counter++)
{if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];} // if
} // for0 1 2 3 4
42 17 42 -8 4 -8smallestSoFar
5counter
![Page 60: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/60.jpg)
Passing Arrays to FunctionsPassing Arrays to Functions
#include <iostream.h>
void modifyArray (int inArray[ ], int arraySize) {// Stuff that changes the array
}
void main ( ) {int myArray [15];modifyArray (myArray, 15);
}
![Page 61: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/61.jpg)
The Overall ConceptThe Overall Concept
• Arrays hold values that are all the same type
• Arrays are static in size• Creating arrays always follows a
format• You can create an array of any type• To initialize or search arrays, you’ll
almost always use a loop
![Page 62: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/62.jpg)
62
More on Initializing Arrays
Initialize all three indexed variables of the array “a”
If fewer values are listed than there are indexed variables, remaining indexed variables are initialized to zero of the array base type
Automatically sized
int a[ 3 ] = { 2, 12, 1 }; int a[ 3 ];a[ 0 ] = 2;a[ 1 ] = 12;a[ 2 ] = 1;
int b[ ] = { 5, 12, 11 }; int b[ 3 ] = { 5, 12, 11 };is equivalent to:
is equivalent to:
Only initializes first 3 elements of 5 element arrayint c[ 5 ] = { 2, 4, 8 };
2 4 8 0 0 Un-initialized elements set to zero
![Page 63: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/63.jpg)
63
Initializing StringsIf string constant is used, null terminator is automatically included
char short_str [ ] = “abc”;
Character array also has indexed variables
Sized automatically to length of string + 1 for the ‘\0’
Change value in short_str to contain all ‘X’ characters
Is equivalent to:char short_str [ 4 ] = “abc”;
Is not equivalent to:char short_str [ ] = {‘a’, ‘b’, ‘c’};
short_str[ 0 ] short_str[ 1 ] short_str[ 2 ] . . .
int index = 0;while ( short_str [ index ] != ‘\0’ ){ short_str [ index ] = ‘X’; index++;}
‘a’ ‘b’ ‘c’ ‘\0’ ‘a’ ‘b’ ‘c’
Element data type
Loop ends when element is \0
![Page 64: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/64.jpg)
64
More Array Processing
Processing array elements is the same as processing other variables
Increment value in score[ 2 ]++ score [ 2 ];int result = score [ 4 ] * 2;
if ( score[ 3 ] < score[ 4 ] )
Loop iterates as long as score[ count ] does not equal 0
int score[ 5 ] = { 7, 8, 9, 10, 0 };
Is the value in score[ 3 ] less than the value in score[ 4 ]?
Initializes result to value of score[ 4 ] times 2
while ( score[ count ] != 0 )
![Page 65: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/65.jpg)
65
Parallel Arrays
Using two or more arrays to represent relationships involving different data types
Elements are integers
const int NUMEMPS;int hours [NUMEMPS];float payRate [NUMEMPS];. . .for ( int index = 0; index < NUMEMPS; index++ ){ cout << “Hours employee #” << ( index + 1 ); cin >> hours[ index ]; cout << “Pay rate employee #” << ( index + 1 ); cin >> payRate[ index ];}
Stores hours worked by each employee
Stores pay rate for each employee
Number of employees
Elements are floats
Same index used to access both arrays
![Page 66: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/66.jpg)
66
Printing Array ContentsUse a loop to display the contents of each array element
int testArr [ 5 ] = { 10, 20, 30, 40, 50 };
Displays address of the array, not the contents
Declare and initialize array
Loop displays value of each element
cout << testArr << endl;
for ( int ct = 0; ct < 5; ct++ ) cout << testArr [ ct ] << endl;
Doesn’t work!
Works!
Exception: Displaying the contents of a char array containing a C-string
char name [ ] = “Ned Nerd”;cout << name << endl;
cout uses \0 when given a char array to determine end of the string
Displays string, not array address
![Page 67: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/67.jpg)
67
Array Elements as Function Arguments
Array element, type int, as the argument
void showVal ( int num );void main ( ){ int testArr [ 5 ] = { 5, 10, 15, 20, 25 }; for ( int ct = 0; ct < 5; ct++ ) showVal ( testArr [ ct ] );}void showVal ( int num ){ cout << num << “ “;}
Array elements can be passed by value or by reference
Parameter is also type int
With each loop iteration, the value contained in testArr[ct] is passed to the function showVal
5 10 15 20 25
Program output:
![Page 68: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/68.jpg)
68
Arrays as Function Arguments
Starting address of array is passed to function showVal
void showVal ( int nums [ ] );void main ( ){ int testArr [ 5 ] = { 5, 10, 15, 20, 25 }; showVal ( testArr );}void showVal ( int nums [ ] ){ for ( int ct = 0; ct < 5; ct++ ) cout << nums [ ct ] << “ “;}
Any changes to parameter nums, effect argument testArr
5 10 15 20 25
Program output:
5 10 15 20 25testArr
0 1 2 3 4
nums[0] nums[1] nums[2] nums[3] nums[4]
![Page 69: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/69.jpg)
69
Arrays as Function ArgumentsUse two arguments: The address of the array
The size of the array
int testArr1 [ 2 ] = { 5, 10 };int testArr2 [ 5 ] = { 5, 10, 15, 20, 25 };int testArr3 [ 7 ] = { 5, 10, 15, 20, 25, 30, 35 };showVal ( testArr1, 2 );showVal ( testArr2, 5 );showVal ( testArr3, 7 );
Modify function showVal to display the contents of an int array of any size
Function calls to showVal
In main:
Address of arraySize of array
void showVal ( int nums [ ], int size ){ for ( int ct = 0; ct < size; ct++ ) cout << nums [ ct ] << “ “;}
New showVal:
![Page 70: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/70.jpg)
70
Arrays as Function Arguments
Array parameters give direct access to the array argument
Changes values in parameter nums and argument testArr
void doubleArr ( int nums [ ], int size );void main ( ){ int testArr [ 5 ] = { 1, 2, 3, 4, 5 }; for ( int ct = 0; ct < 5; ct++ ) cout << testArr [ ct ] << “ “; doubleArr ( testArr, 5 ); for ( int ct = 0; ct < 5; ct++ ) cout << testArr [ ct ] << “ “;}void doubleArr ( int nums [ ], int size ){ for ( int i = 0; i < size; i++ ) nums [ i ] *= 2;}
1 2 3 4 52 4 6 8 10
Program output:
![Page 71: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/71.jpg)
71
Two-dimensional ArraysTwo-dimensional array is several identical arrays put together in the form of a table
score[0][0] score[0][1] score[0][2]
score[1][0] score[1][1] score[1][2]
score[2][0] score[2][1] score[2][2]
column 0 column 1 column 2
row 0
row 1
row 2
Exam scores
Students
float score [ 3 ] [ 3 ];
score [ 1 ] [ 2 ] = 93.2;
cout << score [ 0 ] [ 2 ];
Number of rows Number of columns
Row index
Column index
Assign value to an element
Display value of an element
![Page 72: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/72.jpg)
72
Two-dimensional ArraysNested loops are used to process each element of a two-dimensional array
float score [ 3 ] [ 3 ];for ( int std = 0; std < 3; std++ ){ for ( int exam = 0; exam < 3; exam++ ) { cout << “Student “ << std + 1 << “ , exam “ << exam + 1 << “: “; cin >> score [ std ] [ exam ]; } cout << endl;}
92.3 88.5 83.6
79.2 72.8 ?
? ? ?
0 1 2
0
1
2
Outer loop iterates over rows
Inner loop iterates over columns
Student 1, exam 1: 92.3Student 1, exam 2: 88.5Student 1, exam 3: 83.6Student 2, exam 1: 79.2Student 2, exam 2: 72.8. . .
Program output:
score
Read in an exam scoreColumn index
Row index
![Page 73: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/73.jpg)
73
Two-dimensional Arrays as Function Arguments
Number of columns is specified in a two-dimensional array parameter
void showArr ( int Arr [ ] [ 2 ], int rows );void main ( ){ int table [ 3 ] [ 2 ] = { { 8, 5 }, { 7, 9 }, { 6, 3 } }; showArr ( table, 3 );}void showArr ( int Arr [ ] [ 2 ], int rows ){ for ( int r = 0; r < rows; r++ ) { for ( int c = 0; c < 2; c++ ) cout << Arr [ r ] [ c ] << “ “; cout << endl; }}
8 59 96 3
Program output:
Row 1 Row 2 Row 3
Extra braces that enclose each row’s values are optional
Empty Column indexNumber of rows
Iterates rows
Iterates columns
![Page 74: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/74.jpg)
74
Arrays StringsA two-dimensional array of characters can be used as multiple arrays of strings
char team [ 4 ] [ 9 ] = { “Ned”, “Connie”, “Pat”, “Greg” };
cout << team [ 2 ];
for ( int ct = 0; ct < 4; ct++ ) cout << team [ ct ] << endl;
Name of array with only a row index is the address of that row
Loop displays all names in the array
N e d \0
C o n n i e \0
P a t \0
G r e g \0
0
1
2
3
Pat
NedConniePatGreg
Maximum length of string is 9 – 1 (for null terminator)
Four names, 8 characters long
Programoutput:
![Page 75: Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062317/5a4d1b657f8b9ab0599af96b/html5/thumbnails/75.jpg)
End of Lecture 4End of Lecture 4
• Next Time, more on Arrays
• Program – 2, Due Thurs. 7/7 (classes)
• Program – 3, Due Thurs. 7/21 (arrays)