CSC-201 - Computer Science I Lecture #9: Chapter 10...

28
1/28 Test Results Schedule Miscellanea Chap. 10 Break Hands on Q&A Conclusion References Files CSC-201 - Computer Science I Lecture #9: Chapter 10 (con’t.) Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm October 19, 2016 at 3:04pm

Transcript of CSC-201 - Computer Science I Lecture #9: Chapter 10...

  • 1/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    CSC-201 - Computer Science ILecture #9: Chapter 10 (cont.)

    Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge

    October 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pmOctober 19, 2016 at 3:04pm

  • 2/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Table of contents I

    1 Test Results

    2 Schedule

    3 Miscellanea

    4 Chap. 10

    5 Break

    6 Hands on

    7 Q & A

    8 Conclusion

    9 References

    10 Files

  • 3/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Histograms

    Histogram of questions and misses

  • 4/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Histograms

    Histogram of letter grades

    Test scores are only part of your final grade. See syllabus.

  • 5/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Histograms

    Q & A time.

    The Answer to the GreatQuestion . . . Of Life, the Universeand Everything . . . is. . . forty-two, said DeepThought, with infinite majestyand calm.Douglas Adams, TheHitchhikers Guide to theGalaxy

  • 6/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Schedule for the semester:

    Wk. Date Topic Wk. Date Topic1 08/22 Chaps. 1 2 X 9 10/17 Chap. 102 08/29 Chaps. 3 4 X 10 10/24 Chaps. 10 113 09/05 Chap. 5 X 11 10/31 Chaps. 10 114 09/12 Test Chap. 6 X 12 11/07 Test Chap. 145 09/19 Chap. 7 X1 13 11/14 Chap. 146 09/26 Chap. 8 X 14 11/28 Chap. 187 10/03 Chap. 9 X 15 12/05 Chap. 188 10/10 Test Chap. 10 X 16 12/12 Exam

    Notes:

    1 Class canceled due to electrical fire in instructors home

  • 7/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Programming assignment #004

    File Based Address Book

    Objectives are fairly straightforward:

    Accept two new commands fromthe user

    Commands are:

    + to add a new contact- to remove all contacts that

    match a search string

    Update the data file with new, orremoved contacts

    Submit your source code.

    This is a single person effort (not ateam effort).

    Due by start of class 26 Oct. 2016.

  • 8/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Programming assignment #004

    Q & A time.

    Q: Do you know what the deathrate around here is?A: One per person. Now is the

    time to ask questions.

  • 9/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Corrections and additions since last lecture.

    Completed gradinghomework for chapters 7, 8,and 9

    Graded exam #002

  • 10/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    enums

    A quick review

    1 There are different simpledata types

    2 typedef is a namesubstation, not type creation

    3 Each type has a differentsize (see files section)

    4 A types domain iscontrolled by its size

    5 enums are a type, and havelimited domain

  • 11/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    enums

    Some basics about enums

    enums are:

    1 A simple variable type2 You define the domain of

    values associated with thetype

    3 The domain values must beglobally unique (can bejiggered using namespace

    tricks)

    4 You can create newvariables of your enum type

    5 You can use them as passparameters, return values,loop controls, and incomparisons

    Well have a hands on exercise with enums.

  • 12/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    enums

    Creating enums

    There are a number of different ways to create enum depending onyour need.

    1 enum TYPE { domain };enum TimeZones {EASTERN, CENTRAL, MOUNTAIN,PACIFIC};Creates a new type that can be used where scoping rulespermit.

    2 enum { domain };enum {EASTERN, CENTRAL, MOUNTAIN, PACIFIC};Creates values (nearly like const int) that be used wherescoping rules permit.

  • 13/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    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:

  • 14/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    structs are structured data types

    A structured data type is a type in which each value is a collectionof component items

    The entire collection has a single name

    Each component can be accessed individually

    Used to bundle together related data of various types forconvenient access under the same identifier

  • 15/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    An example

    Image from [2].

  • 16/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    We have created a new User Data Type (UDT)

    Now that we have created a UDT named AnimalType, we candeclare variables of that type:AnimalType thisAnimal;

    AnimalType anotherAnimal;

    These are two totally independent variables, that are made up ofother variables.

  • 17/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    Important things to remember about structs

    1 The struct declaration names atype and names the members ofthe struct

    2 It does not allocate memory for anyvariables of that type!

    3 You still need to declare your structvariables

    4 Scope of a struct:

    If the struct typedeclaration precedes allfunctions, it will bevisible throughout the

    rest of the fileIf it is placed within afunction, only thatfunction can use it

    5 It is common to place struct typedeclarations in a (.h) header fileand #include that file

    6 It is possible for members ofdifferent struct types to have thesame identifiers;

    7 Also a non-struct variable may havethe same identifier as a structuremember

  • 18/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    How to access struct members?

    Dot (period) is the memberselection operatorAfter the struct type declaration,the various members can be usedin your program only when theyare preceded by a struct variablename and a dot Image from [1].

  • 19/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    structs

    Same image.

    Image from [1].

  • 20/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    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++;

  • 21/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    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 awhole. Including:

    Assignment to another struct variable of the same type

    Pass by value, or reference

    Returned from a function

  • 22/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    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) aswell.

  • 23/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Break time.

    Take about 10 minutes.

  • 24/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    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.

  • 25/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Q & A time.

    Q: How many software engineersdoes it take to change a lightbulb?A: None. Well document it inthe manual.

  • 26/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    What have we covered?

    Returned and reviewed test #02Discussed programming assignment#004Continued chapter 10

    Next time: continue Chapter 10, looking at unions and pointers,and start Chapter 11

  • 27/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    References I

    [1] Richard Smith et al., Working draft, standard for programminglanguage c++, ISO/IEC JTC1/SC22/WG21 document N4296 (2015).

    [2] Sylvia Sorkin, Programming and problem solving with c++,Teacher Resource Jones and Barlett Learning, 2014.

  • 28/28

    Test Results Schedule Miscellanea Chap. 10 Break Hands on Q & A Conclusion References Files

    Files of interest

    1 Fibonacci as a simple lookup

    fibonanci-function-lookup.cpp2 Fibonacci as a WHILE loop

    fibonanci-for-loop.cpp3 Fibonacci as a function call

    fibonanci-function.cpp4 Fibonacci as a recursive function

    fibonanci-recursive-function.cpp

    5 Fibonacci as a process queue

    fibonanci-processQueue.cpp

    6 Fibonacci as a state machine

    fibonanci-stateMachine.cpp

    7 variableSize.cpp size of different

    variable types

    8 enumio.cpp working with enum

    types

    9 cars.cpp reading and working

    with structs

    10 cars.dat cars.cpp data file

    #include using namespace std;

    // http://www.miniwebtool.com/list-of-fibonacci-numbers/?number=100

    int fibonaci(int n){ int values[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};

    int returnValue=values[n];

    return returnValue;}

    int main () { int number; int first = 0; int second = 1; int next; cout > number;

    next = fibonaci(number);

    cout