0% found this document useful (0 votes)
58 views19 pages

L17-18 - Array Subtask

Programming in C involves working with arrays. Key tasks include loading, searching, and sorting arrays as well as calculating sums, averages, and extremes. When working with arrays, the program must track how many elements are being used since arrays may be only partially filled. The size of operator returns the total bytes of an array but not the number of elements currently in use. Linear and binary searches are common search algorithms where binary search requires a sorted array. Sorting algorithms range from simple brute force methods to more advanced techniques like bubble, selection, and insertion sorts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views19 pages

L17-18 - Array Subtask

Programming in C involves working with arrays. Key tasks include loading, searching, and sorting arrays as well as calculating sums, averages, and extremes. When working with arrays, the program must track how many elements are being used since arrays may be only partially filled. The size of operator returns the total bytes of an array but not the number of elements currently in use. Linear and binary searches are common search algorithms where binary search requires a sorted array. Sorting algorithms range from simple brute force methods to more advanced techniques like bubble, selection, and insertion sorts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming in C

1
Programming with Arrays
 Subtasks
 Partially-filled arrays
 Loading
 Searching
 Sorting
 Sum, average
 Extremes

2
Partially-filled Arrays (Common Case)
 Must be declared some maximum size
 Program must maintain
 How many elements are being used
and/or
 Highest subscript

3
Sizeof and Arrays
 Operator sizeof returns the total bytes in the
argument
Total elements = sizeof(array) / sizeof(data-
type)
Exampl
e

 Sizeof does not return total bytes being used


 You cannot use sizeof to determine the number of
elements being used in a partially filled array

4
Loading an Array
 Be careful not to overfill

5
Loading a Two-dimensional Array

6
Safer 2D Load

7
Searching an Array
Linear search
Simple
Binary search
Requires sorted array
74? Generally faster for
large arrays
May require the use of
an indicator to denote
found or not found

8
Linear Search Example Using While

9
Linear Search Example Using For

10
Sorting
 Place array into some
order
Ascending or
descending
 Many types
Simple: Brute force
More intelligent: Bubble,
selection, insertion, shell,
comb, merge, heap, quick,
counting, bucket, radix,
distribution, timsort,
gnome, cocktail, library,
cycle, binary tree, bogo,
pigeonhole, spread, bead,
pancake, …

11
Brute Force Sort
 Compare element to all elements below and then
move to next element, swap when appropriate

12
Brute Force Example

13
Bubble/Sinking Sort
 Compare adjacent elements, swap when appropriate
 Stop if no swaps on a pass

14
Bubble Sort Example

15
Sum & Average Example
 Verify positive count before computing average
Protects against division by zero

16
Extremes
 Same techniques as chapter 5 – best:
Assume first is extreme
Compare others to current extreme
Replace extreme when finding new extreme

17
Extremes: Find Maximum
Example

18
Programming in C

T H EE N
D
19

You might also like