CSC-201 - Computer Science I Lecture #9: Chapter 10 (con t.) Dr. Chuck Cartledge October 19, 2016 at 3:04pm 1/28
Table of contents I 1 Test Results 2 Schedule 3 Miscellanea 4 Chap. 10 5 Break 7 Q & A 8 Conclusion 9 References 10 Files 6 Hands on 2/28
Histograms Histogram of questions and misses 3/28
Histograms Histogram of letter grades Test scores are only part of your final grade. See syllabus. 4/28
Histograms Q & A time. The Answer to the Great Question... Of Life, the Universe and Everything... is... forty-two, said Deep Thought, with infinite majesty and calm. Douglas Adams, The Hitchhiker s Guide to the Galaxy 5/28
Schedule for the semester: Wk. Date Topic Wk. Date Topic 1 08/22 Chaps. 1 2 9 10/17 Chap. 10 2 08/29 Chaps. 3 4 10 10/24 Chaps. 10 11 3 09/05 Chap. 5 11 10/31 Chaps. 10 11 4 09/12 Test Chap. 6 12 11/07 Test Chap. 14 5 09/19 Chap. 7 1 13 11/14 Chap. 14 6 09/26 Chap. 8 14 11/28 Chap. 18 7 10/03 Chap. 9 15 12/05 Chap. 18 8 10/10 Test Chap. 10 16 12/12 Exam Notes: 1 Class canceled due to electrical fire in instructor s home 6/28
Programming assignment #004 File Based Address Book Objectives are fairly straightforward: Accept two new commands from the user Commands are: + to add a new contact - to remove all contacts that match a search string Update the data file with new, or removed contacts Submit your source code. This is a single person effort (not a team effort). Due by start of class 26 Oct. 2016. 7/28
Programming assignment #004 Q & A time. Q: Do you know what the death rate around here is? A: One per person. Now is the time to ask questions. 8/28
Corrections and additions since last lecture. Completed grading homework for chapters 7, 8, and 9 Graded exam #002 9/28
Test Results Schedule Miscellanea Chap. 10 Break Hands on Q&A Conclusion References Files enums A quick review 1 There are different simple data types 2 typedef is a name substation, not type creation 3 Each type has a different size (see files section) 4 A type s domain is controlled by its size 5 enums are a type, and have limited domain 10/28
enums Some basics about enums enums are: 1 A simple variable type 2 You define the domain of values associated with the type 3 The domain values must be globally unique (can be jiggered using namespace tricks) We ll have a hand s on exercise with enums. 4 You can create new variables of your enum type 5 You can use them as pass parameters, return values, loop controls, and in comparisons 11/28
enums Creating enums There are a number of different ways to create enum depending on your need. 1 enum TYPE { domain }; enum TimeZones {EASTERN, CENTRAL, MOUNTAIN, PACIFIC}; Creates a new type that can be used where scoping rules permit. 2 enum { domain }; enum {EASTERN, CENTRAL, MOUNTAIN, PACIFIC}; Creates values (nearly like const int) that be used where scoping rules permit. 12/28
enums An example of using typed enum Using the enum statement from before: enum TimeZones {EASTERN, CENTRAL, MOUNTAIN, PACIFIC}; We can create a loop like: 13/28
structs structs are structured data types A structured data type is a type in which each value is a collection of component items The entire collection has a single name Each component can be accessed individually Used to bundle together related data of various types for convenient access under the same identifier 14/28
structs An example Image from [2]. 15/28
structs We have created a new User Data Type (UDT) Now that we have created a UDT named AnimalType, we can declare variables of that type: AnimalType thisanimal; AnimalType anotheranimal; These are two totally independent variables, that are made up of other variables. 16/28
structs Important things to remember about structs 1 The struct declaration names a type and names the members of the struct 2 It does not allocate memory for any variables of that type! 3 You still need to declare your struct variables 4 Scope of a struct: If the struct type declaration precedes all functions, it will be visible throughout the rest of the file If it is placed within a function, only that function can use it 5 It is common to place struct type declarations in a (.h) header file and #include that file 6 It is possible for members of different struct types to have the same identifiers; 7 Also a non-struct variable may have the same identifier as a structure member 17/28
structs How to access struct members? Dot (period) is the member selection operator After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot Image from [1]. 18/28
structs Same image. Image from [1]. 19/28
structs Examples of accessing struct members. thisanimal.age = 18; thisanimal.id = 2037581; cin >> thisanimal.weight; getline (cin, thisanimal.species); thisanimal.name = giant panda; thisanimal.genus[0] = toupper(thisanimal.genus[0]); thisanimal.age++; 20/28
structs There are member operations and aggregate operations A member operation in on a single member of a struct. An aggregate operation is an operation on a data structure as a whole. Including: Assignment to another struct variable of the same type Pass by value, or reference Returned from a function 21/28
structs structs can contain other strucks. structs that contain other structs are called: hierarchical structures. (They are also sometimes called nested structures.) All the rules about simple structs apply to hierarchical structs (member operations, aggregate operations, and dot operations) as well. 22/28
Break time. Take about 10 minutes. 23/28
Practice exercises Programs to load into CodeBlocks and get running: 1 variablesize.cpp find the size of an enum 2 enumio.cpp the program almost works 3 cars.cpp working with structs Others, if you have time. 24/28
Q & A time. Q: How many software engineers does it take to change a light bulb? A: None. We ll document it in the manual. 25/28
What have we covered? Returned and reviewed test #02 Discussed programming assignment #004 Continued chapter 10 Next time: continue Chapter 10, looking at unions and pointers, and start Chapter 11 26/28
References I [1] Richard Smith et al., Working draft, standard for programming language c++, ISO/IEC JTC1/SC22/WG21 document N 4296 (2015). [2] Sylvia Sorkin, Programming and problem solving with c++, Teacher Resource Jones and Barlett Learning, 2014. 27/28
Files of interest 1 Fibonacci as a simple lookup 6 Fibonacci as a state machine fibonanci-function-lookup.cpp 2 Fibonacci as a WHILE loop fibonanci-for-loop.cpp 3 Fibonacci as a function call fibonanci-function.cpp 4 Fibonacci as a recursive function fibonanci-recursive-function.cpp fibonanci-statemachine.cpp 7 variablesize.cpp size of different variable types 8 enumio.cpp working with enum types 9 cars.cpp reading and working 5 Fibonacci as a process queue fibonanci-processqueue.cpp with structs 10 cars.dat cars.cpp data file 28/28