Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Low level collections.

Similar presentations


Presentation on theme: "Arrays Low level collections."— Presentation transcript:

1 Arrays Low level collections

2 Motivation I want to: Read in 30 test scores
Print out grades based on a curve 15%+ over average : A 5%+ over average : B +/- 5% from average : C 5%+ below average : D 15%+ below average : F

3 Motivation Ways to solve Read twice – yuck, why?

4 Motivation Ways to solve 30 variables – yuck, why?

5 Arrays 10 5 8 9 6 7 4 Array : a collection of items Fixed size
Must know at compile time Always the same type 10 5 8 9 6 7 4

6 Arrays 10 9 Array : a collection of items
Fixed size Must know at compile time Always the same type Each element has an index, starting from 0 1 2 3 4 5 6 7 8 10 9

7 Array Syntax Declaring an array: type name[size]; int num[5];
Make 5 consecutive int storage boxes numbered 0-4

8 Array Syntax Declaring an array: type name[size]; Yes: No:
size: must be a constant expression that evaluates to an integer Not a variable!!! Yes: No:

9 Background: The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

10 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 X 2 1

11 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 Y 1.2 10 9 8 7 6 5 4 3 X 2 1

12 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

13 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 c a 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

14 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

15 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 Y 1.2 10 9 8 7 6 5 4 3 X 2 1

16 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 z 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

17 The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

18 Stack Thoughts Works well to efficiently reuse space Requirements
Need to know sizes Can't change the size once something is there

19 Array Implementation Arrays implemented with sequential blocks of memory cells Address Identifier Value 16 15 14 13 12 11 quiz[2] 8 10 9 7 quiz[1] 5 6 4 3 quiz[0] 2 1 1 2 10 5 8

20 g++ g++ has an extension for variable length arrays
Turn off "Enforce standard C++" This works: DO NOT USE

21 Accessing Elements Subscript operator [ ] used to access elements list[5] = 34; //set 6th element to 34 Index can be any expression: //set item at index 3 to //set element at (2i-3) to 58

22 Accessing Elements Elements can be read from or written to:

23 No Bounds Checking Nothing sanity checks your indexes:

24 No Bounds Checking Nothing sanity checks your indexes: 24 23 x 10 22
Address Identifier Value 24 23 x 10 22 21 20 19 nums[4] 18 17 16 3 nums[0] 2 1

25 No Bounds Checking Nothing sanity checks your indexes: 24 23 x 10 22
Address Identifier Value 24 23 x 10 22 21 20 19 nums[4] 18 17 16 3 nums[0] 2 1

26 No Bounds Checking No sanity checks of indexes: May get weird results
May crash

27 Array Initialization Uninitialized arrays start with mystery garbage
Initialization list can be used to set values: double myList[4] = {1.9, 2.9, 3.4, 3.5}; Same as

28 Array Initialization Uninitialized arrays start with mystery garbage
Initialization list can be used to set values: double myList[4] = {1.9, 2.9, 3.4, 3.5}; Don't need to specify size if initializing: double myList[] = {1.9, 2.9, 3.4, 3.5};

29 Array Initialization Insufficient initializers?
Use what is there, set rest to 0 Quick initialize whole array to 0:

30 No Aggregate Operations
Operators do not work on entire arrays No = to whole array:

31 No Aggregate Operations
Operators do not work on entire arrays Printing gives you a memory address

32 Base Address Array variable actually stores base address
Printing shows you base address:

33 Base Address Array variable actually stores base address
Can be used to efficiently find any element: elementLocation = (index * elementSize) + baseAddress Subscript does this recipe: nums[3]  Start at memory location given by nums array. Then go forward 3 units of size.

34 No Aggregate Operations
Operators do not work on entire arrays Or at least don't do what you think < > = don't compare values, compare addresses

35 Work with Elements Moral: Create arrays, work with elements

36 Processing Arrays Arrays generally processed with counting loop:
Count from 0 to size – 1 i is the current index array[i] is the current element

37 Processing Examples Initialize all values to 10:
Count from 0 to size – 1 array[i] is the current element

38 Processing Examples Copy an array Count from 0 to size – 1
array[i] is the current element

39 Processing Examples Total up an array Need variable for total
Count from 0 to size – 1 array[i] is the current element

40 Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest
Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 4 1 2 3 4 5 6

41 Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest
Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 4 1 2 3 4 5 6

42 Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest
Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 3 1 2 3 4 5 6

43 Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest
Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 2 1 2 3 4 5 6

44 Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest
Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 2 1 2 3 4 5 6

45 Processing Examples Find smallest Assume first is smallest
Count from 1 to size – 1 array[i] is the current element

46 Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8
Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next 1 2 3 4 5 6 7 8 9 10

47 Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8
Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next 1 2 3 4 5 6 7 8 9 10

48 Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8
Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next Oooops – we found a problem!!! 1 2 3 4 5 6 7 8 9 10

49 Processing Examples 9 12 15 18 19 20 Check if sorted 1 2 3 4 5 6 7 8
Assume it is sorted Count from 0 to size – 2 – stop early!!! array[i] is current, array[i + 1] is next Don't want last element to become "current " No "next" 1 2 3 4 5 6 7 8 9 12 15 18 19 20

50 Processing Examples Check if sorted Assume it is sorted
Count from 0 to size – 2 stop early!! array[i] is current, array[i + 1] is next


Download ppt "Arrays Low level collections."

Similar presentations


Ads by Google