+ All Categories
Home > Documents > CSC-201 - Computer Science I Lecture #12: Chapter 14...

CSC-201 - Computer Science I Lecture #12: Chapter 14...

Date post: 12-Mar-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
25
1/25 Test overview Test Schedule Miscellanea Chapter 14 Hands on Q&A Conclusion References Files CSC-201 - Computer Science I Lecture #12: Chapter 14 started 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 November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm November 14, 2016 at 5:34pm
Transcript
  • 1/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    CSC-201 - Computer Science ILecture #12: Chapter 14 started

    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

    November 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pmNovember 14, 2016 at 5:34pm

  • 2/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Table of contents I

    1 Test overview

    2 Test

    3 Schedule

    4 Miscellanea

    5 Chapter 14

    6 Hands on

    7 Q & A

    8 Conclusion

    9 References

    10 Files

  • 3/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    This is it.

    Cover chapters 10 and 11

    Exam has been written

    Exam should take about anhour and 30.

    One letter sized sheet ofpaper as “cheat sheet”

    When finished with exam,leave room with answers

    Lecture will start after examends

    Any questions? Almost too late.

  • 4/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    The beatings will continue until morale improves.

    You’ve got:1 hour 30 minutes

  • 5/25

    Test overview Test Schedule Miscellanea Chapter 14 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. 10 X2 08/29 Chaps. 3 – 4 X 10 10/24 Chaps. 10 – 11 X3 09/05 Chap. 5 X 11 10/31 Chaps. 10 – 11 X4 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 Test2 – Chap. 188 10/10 Test – Chap. 10 X 16 12/12 Exam

    Notes:

    1 Class canceled due to electrical fire in instructor’s home

    2 Test covering chapters 14 and 18 was not part of the initialschedules due to an oversight.

  • 6/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Programming assignment #005

    Internal Array Based Address Book

    Objectives are fairly straightforward:

    Maintain all the capability fromprogramming assignment #004

    Output matching search contactsalphabetized by last name

    Support up to 1,000 contacts

    Use an array of structures to dothe work

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

    Due by start of class 9 Nov. 2016.

  • 7/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Programming assignment #006

    N-Queens

    Objectives are easily stated, requiressome thought

    Accept the size of the board

    Find two solutions (if they exist)

    Output the solutions, or specifictext

    Use dynamic data structures

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

    Due by start of class 30 Nov. 2016.

  • 8/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Programming assignment #006

    Q & A time.

    “In the beginning,the universe wascreated. This made alot of people veryangry, and has beenwidely regarded as abad idea.”

    Douglas Adams [1]

    The answer to many questions is42. The only difference is units.

  • 9/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Corrections and additions since last lecture.

    Chapter 12 and 13 (someideas abbreviated)

    Vocabulary and conceptsstill important

    Things that were skippedwill be part of CSC-210

    Important ideas aboutclasses, inheritance, andpolymorphism

  • 10/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Background

    Necessary ideas and concepts

    Automatic vs. manual variables (i.e., the compiler takes careof them vs. we as programmers take care of them)

    Pointers contain the memory location of something

    A pointer can contain the memory location of an automatic ormanual variable

    A variable can be simple or complex

    There is a way to create and delete manual variables

  • 11/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Background

    Leonardo Fibonacci

    1175 circa - 1235 circa,Pisa, Italy

    Most talented Westernmathematician of theMiddle Ages

    Popularized theHindu-Arabic numeralsystem

    Image from [4].

    Our interest is his Fibonacci sequence.

  • 12/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Background

    Fibonacci sequence

    Rabbits, rabbits, and more rabbits

    1 Rabbits can mate when 1month old

    2 Each mating results in amale and female

    How many pairs will there be inone year?

    Image from [4].

    Seemingly simple sequence appears in lots of places.

  • 13/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Background

    Fibonacci sequence in unexpected places

  • 14/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Background

    Practice exercises

    Variations on a theme:

    1 Fibonacci as a simple lookup –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 process queue –fibonanci-processQueue.cpp

    We’ll be “diddling” fibonanci-processQueue.cpp

  • 15/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Some vocabulary[2]

    A list is a varying length, linear collection of (probably)homogeneous elements

    Linear means:

    Each list element (except the first) has a unique predecessor,andEach list element (except the last) has a unique successor

    ADT means abstract data type

  • 16/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Things that we can and will do with ADTs

    Constructors – create a new instance (object) of an ADT

    Transformers – change the state of one or more of the datavalues of an instance

    InsertDeleteSort

    Observers – allow client to observe the state of one or more ofthe data values of an instance without changing them

    IsEmptyIsFullLengthIsPresent

    Iterators – allow client to access the data values in sequence

    Reset – prepares for the next iterationGetNextItem – returns the next item in the sequence (list)

  • 17/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Bringing the pieces together

    In an automatic array (like we’ve talked about to date) isstored in contiguous memory locations, implementingtransformer operations by moving list items around in thearray

    In a linked list items are not necessarily stored in contiguousmemory locations, and avoids excessive data movement fromtransformations

    A linked list is a list in which the order of the components isdetermined by an explicit link member in each node

    Each node is a struct containing a data member and a linkmember that gives the location of the next node in the list

    A dynamic linked list is one in which the nodes are linkedtogether by pointers and an external pointer (or head pointer)points to the first node in the list

  • 18/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Pictures are worth 1K words

    Linked list elements point toone another

    There can be more than onepointer

    The pointers can be neverending

    The pointers can point inboth directions

    Inside each node is a pointer

    Image from [3].

  • 19/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Details about the fibonanci-processQueue.cpp program

    The structures are useful.

    queueControlStruct will be a nodein the list

    funcStruct will be a functionpointed to by a list node

    globalDataStruct contains all dataat global scope

    Other things:

    malloc gets memory from the OS

    memset sets memory to a value

    free returns memory to the OS

  • 20/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Details

    Same image.

  • 21/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Practice exercises

    Programs to load into CodeBlocks and get running:

    1 fibonanci-processQueue.cpp – Change the program toincorporate a constructor, the transformers, and the iterators

    We’ll divide the source into two files. One a header, one theprogram.

  • 22/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Q & A time.

    Q: How many journalists does ittake to screw in a light bulb?A: Three. One to report it as aninspired government program tobring light to the people, one toreport it as a diabolicalgovernment plot to deprive thepoor of darkness, and one to wina Pulitzer prize for reporting thatElectric Company hired a lightbulb-assassin to break the bulb inthe first place.

  • 23/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    What have we covered?

    Test covering chapters 10 and 11Started chapter 14 taking aboutdynamic data and linked lists

    Next time: continuing chapter 14, copying, inserting, deleting, andsorting

  • 24/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    References I

    [1] Douglas Adams, The ultimate hitchhiker’s guide, Wings books,1996.

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

    [3] PumpkinProgrammer Staff, C++ tutorial: Intro to linked lists,http://pumpkinprogrammer.com/2014/06/13/c-

    tutorial-intro-to-linked-lists/, 2014.

    [4] Wikipedia Staff, Leonardo fibonacci,https://it.wikipedia.org/wiki/Leonardo_Fibonacci,2016.

    http://pumpkinprogrammer.com/2014/06/13/c-tutorial-intro-to-linked-lists/http://pumpkinprogrammer.com/2014/06/13/c-tutorial-intro-to-linked-lists/https://it.wikipedia.org/wiki/Leonardo_Fibonacci

  • 25/25

    Test overview Test Schedule Miscellanea Chapter 14 Hands on Q & A Conclusion References Files

    Files of interest

    1 Fibonacci as a simple lookup

    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 functionfibonanci-recursive-function.cpp

    5 Fibonacci as a process queue

    fibonanci-processQueue.cpp

    6 Fibonacci as a state machine

    fibonanci-stateMachine.cpp

    7 006.pdf – programming assignment

    #6

    #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

  • CSC-201 Fall 2016Assignment #6: N-Queens, or Which Way To Go?

    Due: Nov. 30, 2016

    Dr. Chuck Cartledge

    November 8, 2016 at 9:09am

    Contents

    List of Figures 1

    1 Explanation 1

    2 Assignment 3

    3 What to submit 3

    4 References 3

    List of Figures

    1 One solution for the 8-Queens problem. . . . . . . . . . . . . . . . . . . . . . 22 Another solution for an 8-Queens placement. . . . . . . . . . . . . . . . . . . 43 A solution for a 9-Queens placement. . . . . . . . . . . . . . . . . . . . . . . 44 A solution for a 10-Queens placement. . . . . . . . . . . . . . . . . . . . . . 4

    1 Explanation

    Your absent minded professor has given up on managing his address book, and has turnedhis attention to things of logic. As a one-time chess player, he is interested in the N-Queensproblem.

    The N-Queens problem is one of logic and insight. N-Queens comes from the morecommon 8-Queens.

  • Figure 1: One solution for the 8-Queens problem.

    “The eight queens puzzle is the problem of placing eight chess queens on an8×8 chessboard so that no two queens threaten each other. Thus, a solutionrequires that no two queens share the same row, column, or diagonal. The eightqueens puzzle is an example of the more general n queens problem of placingn non-attacking queens on an n×n chessboard, for which solutions exist for allnatural numbers n with the exception of n=2 and n=3.”

    W. Staff [1]

    One solution for 8-Queens problem (see Figure 1).1 There are multiple solutions for theproblem. While the problem of placing 8 queens on an 8×8 chess board is well solved, andeasily found; generalizing it to an N×N chessboard is slightly more challenging.

    A queen on a chess board can attack any piece that is on the same row, column, or oneof the diagonals that the queen is on. The solution for an N-Queens problem is to place Nqueens on an N×N chess board such that none of them can attack each other.

    1All figures are from [1].

    2

  • 2 Assignment

    The program will do the following things:

    1. Prompt the user to enter n (the size of the board) using cout. n can be between 0 and100.

    2. Find two solutions for the size of the board (there may not be two solutions)

    3. For each solution found, output the row for each queen to cerr

    4. If no solution is found, output the string “No solution” to cerr

    5. Document your source file in accordance with Appendix F of the text

    For example; two solutions for an 8×8 board could be (see Figures 1 and 2):4 6 8 2 7 1 3 54 8 1 3 6 2 7 5Your program needs to use dynamic data structures (as per chapter 11) to solve this

    problem. Solutions for the N-Queens problem exist for N equal to one, and N greater thanthree (see Figures 2 through 4).

    3 What to submit

    The C++ source code that meets the assignment requirements.

    4 References

    [1] Wikpedia Staff, Eight queens puzzle, https://en.wikipedia.org/wiki/Eight_queens_puzzle.

    3

    https://en.wikipedia.org/wiki/Eight_queens_puzzle

    https://en.wikipedia.org/wiki/Eight_queens_puzzle

  • Figure 2: Another solution for an 8-Queens placement.

    Figure 3: A solution for a 9-Queens placement.

    Figure 4: A solution for a 10-Queens placement.

    4

    "Chuck Cartledge"

    Test overviewTestScheduleProgramming assignment #005Programming assignment #006

    MiscellaneaCorrections and additions since last lecture.

    Chapter 14BackgroundDetails

    Hands onQ & AConclusionReferencesFiles


Recommended