Download presentation
Presentation is loading. Please wait.
1
Module 5 Working with Arrays
2
@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 @UMBC Training Centers 2013
3
@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: @UMBC Training Centers 2013
4
@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: @UMBC Training Centers 2013
5
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]); @UMBC Training Centers 2013
6
@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 @UMBC Training Centers 2013
7
@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 @UMBC Training Centers 2013
8
@UMBC Training Centers 2013
const Qualifier Specified that an initialized variable won’t change values const double pi = ; const char digits[ ] = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ }; @UMBC Training Centers 2013
9
@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 @UMBC Training Centers 2013
10
@UMBC Training Centers 2013
Exercises 30 mins From text, pg 118 #4 and 5 @UMBC Training Centers 2013
11
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!! @UMBC Training Centers 2013
12
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]); @UMBC Training Centers 2013
13
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 @UMBC Training Centers 2013
14
@UMBC Training Centers 2013
Let’s take a look CB – PartialArrays @UMBC Training Centers 2013
15
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) @Copyright UMBC Training Centers 2012
16
@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 @Copyright UMBC Training Centers 2012
17
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 @Copyright UMBC Training Centers 2012
18
@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 . . . @Copyright UMBC Training Centers 2012
19
Exchange Sort Progression
First Pass Second Pass Third Pass More Passes… @Copyright UMBC Training Centers 2012
20
@UMBC Training Centers 2013
Let’s take a look C::B – Exchange sort @UMBC Training Centers 2013
21
@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 @Copyright UMBC Training Centers 2012
22
@UMBC Training Centers 2013
Binary Search Videos @UMBC Training Centers 2013
23
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 @Copyright UMBC Training Centers 2012
24
@UMBC Training Centers 2013
Let’s take a look C::B – BinarySearch @UMBC Training Centers 2013
25
@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 @UMBC Training Centers 2013
26
@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. @UMBC Training Centers 2013
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.