Module 5 Working with Arrays
@UMBC Training Centers 2013 What is an array? Data structure a collection of elements Ordered first, second, third, … Homogenous all elements are the same type Fixed Size defined when your code compiles www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Declaring Arrays type name[ size ]; int grades[ 6 ]; Use named constant for array size #define MAX_GRADES 6 int grades[MAX_GRADES]; 1 2 3 4 5 ??? grades: www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Initializing Arrays int scores[5] = {99, 44, 22, 11, 10}; 1 2 3 4 99 44 22 11 10 scores: char word [ ] = {‘B’, ‘o’, ‘b’}; 1 2 ‘B’ ‘o’ word: double costs[5] = {5.45, 7.89, 1.01}; 1 2 3 4 5.45 7.89 1.01 costs: www.umbctraining.com @UMBC Training Centers 2013
Accessing Array Elements array_name[ index ] First element has index = 0 Max index for an array with N elements is N – 1 int cats[MAX_CATS] = {2, 4, 6, 8, 10, 12}; int nrCats = cats[4]; cats[5] = cats[5] – cats[0] + 6; printf(“ %d\n”, cats[1]); www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Out of Bounds int ages[4] = {12, 24, 36, 48}; ages 1 2 3 ??? 12 24 36 48 int myAge = ages[5]; ages[-2] = 14; Compiler does not check validity of index www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 For Loops and Arrays #define MAX_CATS 6 int i, cats[MAX_CATS]; for(i = 0; i < MAX_CATS; i++) { cats[i] = 0; } for(i = MAX_CATS-1; i >= 0; i--) { printf(“%d\n”, cats[i]); } // displays backwards www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 const Qualifier Specified that an initialized variable won’t change values const double pi = 3.14159; const char digits[ ] = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ }; www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Let’s take a look CB – arrays1 Note the use of #define for array sizes – if array size changes, just change #define in one place IDOM.. i = 0; i < size; i++ CB-Program7.2 (counting responses) Initialization with a for-loop Not using [0] to avoid i-1 coding CB-Program7.3 (calc and store Fibonacci) Discuss the rabbit story (text) No need to initialize entire array www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Exercises 30 mins From text, pg 118 #4 and 5 www.umbctraining.com @UMBC Training Centers 2013
Entering values into an array First need to declare the array Use a for loop to ask for values Enter value Still uses same scanf setup Use another for loop to Display values enter DOES NOT USE SAME printf!! www.umbctraining.com @UMBC Training Centers 2013
Entering values into an array int scores[10]; for(int i = 0; i < 10; i++) { printf("Please enter the score: "); scanf("%d", &scores[i]); } printf("\nThese are the scores you entered:\n "); printf("%d\n", scores[i]); www.umbctraining.com @UMBC Training Centers 2013
Partially Filled Arrays Capacity (size) determined by declaration Program must track number of actual elements int studentGrades[ 10 ]; int nrGrades = ; 4 3 1 2 1 2 3 4 5 6 7 8 9 34 22 99 88 www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Let’s take a look CB – PartialArrays www.umbctraining.com @UMBC Training Centers 2013
Fundamental Array Operations Search Does the array contain a specified element? Sort Arrange the array elements in order from low to high (or high to low) www.umbctraining.com @Copyright UMBC Training Centers 2012
@Copyright UMBC Training Centers 2012 Linear Search Examine elements of the array starting at the first element until The target element is found – SUCCESS There are no more elements – FAILURE Use a while-loop with a compound conditional Check if target was found www.umbctraining.com @Copyright UMBC Training Centers 2012
Linear Search Progression Search for 39 1 2 3 4 5 6 7 8 9 20 12 87 44 39 6 45 22 68 77 www.umbctraining.com @Copyright UMBC Training Centers 2012
@Copyright UMBC Training Centers 2012 Exchange Sort Compare the first element with all other elements. If the other element is smaller, exchange it with the first element. The first element is now the smallest Compare the second element with all other elements (except the first). If the other element is smaller, exchange it with the second element. The second element is now the second smallest Compare the third element with all other elements (except the first and second). If the other element is smaller, exchange it with the third element. The second element is now the third smallest . . . www.umbctraining.com @Copyright UMBC Training Centers 2012
Exchange Sort Progression 34 -5 6 0 12 100 56 4 First Pass -5 34 6 0 12 100 56 4 Second Pass -5 6 34 0 12 100 56 4 -5 0 34 6 12 100 56 4 Third Pass -5 0 6 34 12 100 56 4 -5 0 4 34 12 100 56 6 More Passes…. -5 0 4 6 12 34 56 100 www.umbctraining.com @Copyright UMBC Training Centers 2012
@UMBC Training Centers 2013 Let’s take a look C::B – Exchange sort www.umbctraining.com @UMBC Training Centers 2013
@Copyright UMBC Training Centers 2012 Binary Search Fast search for specified target element Requires sorted array Algorithm Look at the middle element If the middle element is the target, stop If the target > middle element, look at the middle element of the upper “subarray” If the target < middle element, look at the middle element of the lower“subarray” Continue until found or all elements examined www.umbctraining.com @Copyright UMBC Training Centers 2012
@UMBC Training Centers 2013 Binary Search Videos https://www.youtube.com/watch?v=F1ec0Omt9z8&list=PLC7fNkE1QplZo_sAbUaJpyQGt2Ni7CRZM&index=8 https://www.youtube.com/watch?v=7lwau5tLUqA www.umbctraining.com @UMBC Training Centers 2013
Binary Search Progression Search For 68 1 2 3 4 5 6 7 8 9 5 12 16 23 36 44 45 56 68 77 www.umbctraining.com @Copyright UMBC Training Centers 2012
@UMBC Training Centers 2013 Let’s take a look C::B – BinarySearch www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Exercises From the Text -- pg 117 #3 – Modify program 7.2 (do NOT use break) -- #4 – calc average of 10 floating point values- AM “hard code” values into an array of size 10 #5 – predict output, the code and verify - AM Ex1-HighestQuiz.docx Ex2-Sets.docx Ex3-SieveOfE.docs (see text #7) Ex7-PartialArrays Rewrite program7-4.c using a while-loop www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 If you have any comments about this video, please use the contact information in your registration materials to let us know. www.umbctraining.com @UMBC Training Centers 2013