Download presentation
Presentation is loading. Please wait.
Published byBeverly Booker Modified over 9 years ago
1
String Review Strings are just arrays of charecters Since strings are arrays: –You cannot compare them with >, >=, !=, etc –You cannot copy/assign them with = –There is no inherent way to determine the end of the array Keep an extra variable/data for the string length Use a sentinel to indicate the end of the array C uses a special character, ‘\0’ (much like ‘\n’ is a character) to represent the end of a string
2
Strings Strings are so commonly used that an entire library of string functions exist in Hint for remembering the order of strcpy() –In an assignment statement, the left-hand side is the destination, the right-hand side is the source a = b; –The same principle holds for strcpy() strcpy(dest, src); strcmp() will return to you a negative number if the first argument is less, a positive number of the first argument is greater, and 0 if they’re equal.
3
Structs Structs –A better way to group information –More reusable - you can think of the data as one unit, without needing to worry about the details of its internals –Unlike arrays, a struct can be composed of differing types Structs are user-defined types which group together logically-related data. Structs are used exactly like you use other types –Structs as variables –Pointers to structs –Structs as return values –etc.
4
Structs again Function definitions specify its behaviour; however, the function is not executed until it is called Struct definitions (the thing with the typedef ) are like functions: the definition specifies its components, but no memory is allocated until a variable of that type is created
5
Une Troisieme fois Struct definitions are much like cookie recipes: –They specify components, but you can’t eat a recipe –You can’t talk about “the cookie’s chocolate chips” until you have a specific cookie in mind! typedef struct { int i; char c; } myStructType; myStructType.i = 3; int = 3; double d = 3.0; myStructType m; m.c = ‘s’;
6
Sorting Input: list, listSize for i == 0 to listSize /* Find the smallest */ for j == i to listSize find smallest elt e swap i th elt and e This algorithm “selects” the smallest element, so it’s commonly referred to as “selection sort” Input: list, listSize for i == 0 to listSize for j == 0 to i find where the i th elt belongs insert the i th elt, shifting other elts up as necessary This algorithm “inserts” elements into the proper location, so it’s called “insertion sort.” You probably use this algorithm when sorting a hand of cards! A very important topic in computer science!
7
Why selection sort works Begin with the assumption that the array is unsorted The smallest element in the entire array has to be the first element in the final sorted array Walk through the unsorted array until you find the smallest elt, and swap that elt with the first The second element of the sorted array must be the second smallest elt of the entire array. We know that smallest elt is already in the first index, so the second smallest elt is the smallest elt in the remaining 2 - arraySize elts In general, selection sort searches the i - arraySize elts for the i th smallest elt
8
Challenge Re-write selection sort to sort an array of StudentRecord ’s! What are the two most important operations in selection sort? How will you determine the ordering of the StudentRecord ’s? Is sorting studentRecord ’s really that hard? Could you use insertion sort instead of selection sort?
9
The moral There are many ways/algorithms of doing one thing –How many ways could you draw an enemy in Hw4? There are many ways of storing/representing data –Could you sort CD structs? This is why it’s important to learn the patterns behind what you program. Syntax/language/platform is irrelevant as long as you understand the concepts!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.