csc 237 - data structures, fall, 2008 welcome to data structures! tuesday, september 2 uses...

17
CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Upload: anissa-doyle

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Who are you? Wide-awake Kutztown University computer science students who want to learn to design and implement data structures and algorithms in C++ at 8:00 AM three days a week! You have already taken CSC 125, CSC 136, Computer Science II, earning >= C. This course extends the topics developed in CSC 135. Also covered are: concepts of data abstraction, encapsulation, recursion; search and sort methods; and simple data structures. (Ah, this is the fine print for CSC 136!)

TRANSCRIPT

Page 1: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

CSC 237 - Data Structures, Fall, 2008

Welcome to Data Structures!Tuesday, September 2 uses

Monday’s schedule!

Page 2: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Who am I?

• Dr. Dale E. Parson (a.k.a. Professor Parson), http://faculty.kutztown.edu/parson/

• In a previous life I was an AT&T technician and then a Bell Labs software engineer for over 25 years. Also, I consult as Amplified Computing.

• I have taught at Albright College, Millersville University and Lehigh University.

• This is, in my opinion, the central course of the CSC curriculum. I have taught it at Albright and Millersville.

Page 3: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Who are you?

• Wide-awake Kutztown University computer science students who want to learn to design and implement data structures and algorithms in C++ at 8:00 AM three days a week!

• You have already taken CSC 125, CSC 136, Computer Science II, earning >= C.

• This course extends the topics developed in CSC 135. Also covered are: concepts of data abstraction, encapsulation, recursion; search and sort methods; and simple data structures. (Ah, this is the fine print for CSC 136!)

Page 4: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

What are we planning to do?• We will learn to build the data structures and

algorithms that are at the core of substantial software systems.

• Containers such as lists, tables, stacks, queues, trees• Sets and mappings from keys to values• Algorithms for sorting or otherwise organizing data• Algorithms and file I/O techniques for persistent storage

• We will use modular construction techniques, with the assistance of C++ abstract classes.

• We will use the standard template library (STL) for some projects.

Page 5: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

References• First day handout (syllabus)

• GNU make http://www.gnu.org/software/make/manual/

• GDB http://www.gnu.org/software/gdb/documentation/

• http://faculty.kutztown.edu/parson

Page 6: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

An example makefile• makefile in ~parson/ DataStructures /lecture1/intro

all: buildTARGET = printdemoDEBUG = 1include $(HOME)/makelibbuild: $(TARGET)

$(TARGET): $(OBJFILES) $(CPPCC) $(OBJFILES) -o $@

clean: subclean /bin/rm -f $(TARGET) $(TARGET).out $(TARGET).dif

test: $(TARGET) ./$(TARGET) a b c d e > $(TARGET).out diff $(TARGET).out $(TARGET).ref > $(TARGET).dif

Page 7: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

makelib in my $HOME/• makelibCPPCC= g++CXXFILES := $(wildcard *.cxx)OBJFILES := $(subst .cxx,.o,$(CXXFILES))DEFFLAGS =ifeq ($(DEBUG),1)DEBUGFLAG = -gelseDEBUGFLAG =endifINCFLAGS = -I.CPPFLAGS= $(DEFFLAGS) $(INCFLAGS) $(DEBUGFLAG)

%.o : %.cxx $(CPPCC) -c $(CPPFLAGS) $<

subclean: /bin/rm -f *.o

Page 8: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Some other files in my $HOME/• .bash_profile-bash-3.00$ cat .bash_profileexport EDITOR=/usr/local/bin/vimexport VISUAL=/usr/local/bin/vimalias vi=vimalias make=gmake• .vimrc-bash-3.00$ cat .vimrcset aiset ts=4set sw=4set expandtabset sta

Page 9: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Interface definitions split source along module / driver boundaries.

• printargs.h defines the interface to its module#ifndef PRINTARGS_H#define PRINTARGS_H#include <stdio.h>/* Function: printargs Prints an array of strings to an output file. Parameters: outfile: output file for writing. stringv: array of \0-terminated strings to be printed. stringc: numbers of strings in stringv Return value: none Side Effects: none Preconditions: The outfile must be a non-NULL, open FILE. stringc must equal the number of strings in stringv. Each string in stringv must be terminated with a \0 character. Postconditions: none (no return values from this function). Invariants: none*/voidprintargs(FILE *outfile, const char **stringv, const int stringc);#endif

Page 10: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Implement the interface in a .cxx file.• printargs.cxx implements the module of printargs.h/* printargs.cxx -- Function implementations for printargs.h.

CSC237, Fall, 2008, Dr. Dale Parson, Class 1 example.

This is a simple demo library module that prints an array of strings to an output file.*/

#include "printargs.h"

voidprintargs(FILE *outfile, const char **stringv, const int stringc) { int i ; for (i = 0 ; i < stringc ; i++) { fputs(stringv[i], outfile); fputs("\n", outfile); }}

Page 11: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Invoke the interface from a client module or test driver.

• printmain.cxx is the test driver/* printmain.cxx -- main function that invokes printargs.

CSC237, Fall, 2008, Dr. Dale Parson, Class 1 example.

This is a simple driver for a demo library module that prints an array of strings to an output file.*/

#include "printargs.h"

intmain(int argc, const char *argv[]) { printargs(stdout, argv, argc); return 0 ;}

Page 12: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Makefile-driven testing• makefile is structured for testing-bash-3.00$ make clean test/bin/rm -f *.o/bin/rm -f printdemo printdemo.out printdemo.difg++ -c -I. -g printargs.cxxg++ -c -I. -g printmain.cxxg++ printargs.o printmain.o -o printdemo./printdemo a b c d e > printdemo.outdiff printdemo.out printdemo.ref > printdemo.dif

Page 13: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

GDB for debugging-bash-3.00$ gdb --args printdemo a b c(gdb) break mainBreakpoint 1 at 0x107f4: file printmain.cxx, line 13.(gdb) break printargsBreakpoint 2 at 0x10780: file printargs.cxx, line 14.(gdb) runStarting program:

/export/home/faculty/parson/UnixSysProg/lecture1/printdemo a b cBreakpoint 1, main (argc=4, argv=0xffbffc54) at printmain.cxx:1313 printargs(stdout, argv, argc);14 (gdb) p argc15 $1 = 416 (gdb) p &argc17 $2 = (int *) 0xffbffc34

Page 14: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

GDB continued(gdb) contContinuing.Breakpoint 2, printargs (outfile=0x20b20, stringv=0xffbffc54, stringc=4) at printargs.cxx:1414 for (i = 0 ; i < stringc ; i++) {(gdb) step15 fputs(stringv[i], outfile);(gdb) next/export/home/faculty/parson/UnixSysProg/lecture1/printdemo16 fputs("\n", outfile);(gdb) next14 for (i = 0 ; i < stringc ; i++) {(gdb) p i$3 = 0(gdb) p &i$4 = (int *) 0xffbffb6c

Page 15: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

C++ interfaces and implementation classes

• ~parson/DataStructures/lecture1/oo_variant• We will walk through code and test execution in class.

• C++ abstract classes are used as interfaces.• Derived C++ concrete classes provide

implementation.• Client code, including test drivers, use an

interface without coding dependencies on underlying implementation class.

Page 16: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Unified Modeling Language (UML) Class Diagram of oo_variant

print_main printmain.cxx

<<interface>> printargs_interface.h

printargs_interface printargs_interface.cxx

printargs(outfile : file, stringv : string[], stringc : int)

printargs_simpleimpl printargs_simpleimpl.h printargs_simpleimpl.cxx

Page 17: CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!

Programming practices

• Always check and handle error return codes!• malloc() and new are exceptions that we will discuss.• Error logs, exit codes and C++ exceptions are useful.

• Buffer overruns are annoying and dangerous• See man page for gets(), for example.

• Make malloc/free or new/delete symmetrical.• Always use { curly braces } for control blocks.• Use both healthy and degenerate tests.

» Ignoring these rules will cost you points.