CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
Advertisements

2. C# Language Fundamentals
Fundamentals of Computer and programming in C
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
CISC Data Structures Ben Perry University of Delaware Summer 2011.
Grade 2 Created by; Kim Smith
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Jan. 20 th, 2009.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Instructor Dr. Ken Rodham –3370 TMCB – –Office Hours: By Appointment.
COMP171 Data Structures and Algorithm Huamin Qu Lecture 1 (Sept. 1, 2005)
COMP171 Data Structures and Algorithm Qiang Yang Lecture 1 ( Fall 2006)
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Data Structures and Programming.  Today:  Administrivia  Introduction to 225, Stacks  Course website: 
CIS 199 Test 01 Review. Computer Hardware  Central Processing Unit (CPU)  Brains  Operations performed here  Main Memory (RAM)  Scratchpad  Work.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
CS 2430 Day 1. Agenda Load NetBeans Introduction Syllabus Review some array operations.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 2/10/2010.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CS-2851 Dr. Mark L. Hornick 1 CS-2852 Data Structures Dr. Mark L. Hornick Office: L341 Phone: web: people.msoe.edu/hornick/
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CS 456 Advanced Algorithms Where: Engineering Bldg When: Monday & Wednesday 12:00 – 1:15 p.m. Texts: Algorithm Design, Jon Kleinberg & Eva Tardos.
CSC 107 – Programming For Science. The Week’s Goal.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Primitive Variables.
Data Types Declarations Expressions Data storage C++ Basics.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
CMSC 2021 CMSC 202 Computer Science II for Majors Spring 2003 Mr. Frey (0101 – 0104) Mr. Raouf (0201 – 0204)
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
CSC 205 Java Programming II Introduction. Topics Syllabus Course goals and approach Review I Java language fundamentals.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Structures By Dr. Mehedi Masud ِAssociate Professor, Computer Science Dept. College of Computers and Information Systems Taif University 1.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Computing Fundamentals
Starting JavaProgramming
An overview of Java, Data types and variables
B065: PROGRAMMING Variables 1.
Variables and Constants
Presentation transcript:

CISC Data Structures Ben Perry University of Delaware Summer 2011

Syllabus Details Office hours: Smith hall 103 · Wednesdays 11:00 am - 2:30 pm · or by appointment Teaching assistant: Vincent Ly Text: Data structures and algorithm analysis in c++ - Weiss, Mark AllenData structures and algorithm analysis in c++

Topics Covered Data type abstraction, recursion, arrays, stacks, queues, multiple stacks and linked lists, dynamic storage management, garbage collection, trees, graphs, tables, sorting and searching.

Grading Quizzes 10% - lowest 5 scores dropped Midterm 15% Final 25% Project 20% Labs 30%

Labs -Lab every week except 4 th of July week. -Collaboration is allowed, copying code is not. -Should be able to complete during lab -Later labs will be more challenging

Late Policy Each lab is due at 11:59 PM the Sunday after it was assigned. Five free late days After late days, 50% penalty for late assignments and must be turned in within 48 hours of the deadline.

Upkeep No quiz today. Im not that mean. There is lab today 5 minute break at the top of the hour Slides available online

Outline C++ Basics C++ Memory Management

Comments // This is a one-line comment /* this is a multi-line comment. */

Constants Integer – Keyword int 0, 24, -320 Octal integer – Prefix with Hexadecimal integers – Prefix with 0x Floating points: , 2.7e14

Constants Characters – a, \n Strings – abc Suffixes can be applied to integer constants (U for unsigned, L for long) Several escape codes.

Variables Variables are named locations that can hold values of certain types. Variables are not instantiated in C++ Variables are created using a declaration statement, including the type of the variable. int a,b,c; // declares three int variables int d = 50; // declarations can be instantiated.

Native Data Types Integer – int Floating points – double, float Character – char 'a' 'b' '\0 Native data types can be signed / unsigned (positive or negative, or only positive), or short, long (different ranges of values)

Native Data Types Boolean (bool) – true or false. Enumerations – custom data types that have an explicit range of values. enum Day {monday, tuesday, wednesday, thursday, friday, saturday, sunday}; Day currentDay = monday;

Assignments Once declared, variables can be assigned using an expression: int variable = 0; variable = 25; variable += 25; variable++;

Other operators var++ gets the value of var and then increments the value, but the old value is still used in an expression. int var = 0; cout << var++; // prints 0, but var=1 ++var first increments value of var and then uses the new value. int var = 0; cout << ++var; // prints 1.

Other operators -var negates var ~var uses bitwise negate + / - * simple arithmetic operators % returns the remainder after dividing. For example, 5 % 2 is equal to 1. Also called modulo. << bit-shift left (overloaded as IO stream output) >> bit-shift right (overloaded as IO stream input)

Other operators && and || or ! logical negation < less than <= less than or equal >, >= greater, greater or equal == equals (not to be confused with =, assignment) condition ? true branch : false branch

Strings C-style strings are simply character arrays with the last character being 0. (Not to be confused with 0, which is actually 48 in ascii). In C++, there is a rich string library #include string myString = Some text; cout << myString << endl;

I/O Streams C++ has the notion of I/O streams, such as file IO, console io, network io, and etc. Well use console IO quite a bit at first Output streams and input streams are two different objects. cout stands for console out cin stands for console in

Using cout, cin #include cout, cin, and a host of other objects and classes belong to the namespace std. To access cout, you can either prepend each use with std:: std::cout << hi << std::endl Or insert using namespace std

Writing and Reading Streams To write to any stream, use: – stream << data; For example, to write to the screen: cout << Hello! << endl; To read in, we use: – stream >> variableToHoldData; For example, to read from the keyboard: cin >> inputVariable;

If / then if (variable == value) { // true branch } else{ // false branch, optional }

Switch switch(currentDay){ case Monday: break; case Tuesday: break; case Wednesday: case Thursday: dostuff(); break; default: };

For loop for (initialize, condition, increment){ } Example: for (int i = 0; i < 10; i++){ cout << i << endl; }

While loop while (condition){ // dostuff } Example: while (i < 200){ i*=2; cout << i << endl; }

Arrays Arrays are a contiguous block in memory partitioned into individual units of the given type. Example: int interestingDays[3]; // declares an array of size 3 interestingDays[0] = Friday; interestingDays[1] = Saturday; interestingDays[2] = Sunday; int badDays[2] = {Monday, Tuesday};

Pointers Pointers are variables that hold addresses of places in memory that contain the actual information. Declared with an asterisk (*) int *someInteger; double *someDouble;

More on Pointers Pointers can be subscripted like an array - arrays are technically pointers. The value that pointers point to can be dereferenced by using the * operator. Pointers are typically used to represent memory persistent beyond the local scope.

More on Pointers Pointers must point to legal addresses, which may mean allocating memory (more on that later) Fields can be accessed using the pointer operator ->

Functions Functions have a return type, a method name, and a formals list (parameters list). int performCalc(int number1, int number2){ return number1 * number2 + (number1 / 50); } Return types can be void, as can be parameters. void doStuff(void){ }

Function Prototype Before you can use a method, it must first be prototyped or defined. Declare return type, name, and parameter list. Implement it elsewhere. int foo(); // prototype int foo(){ // implementation // do stuff here }

Local Variables Variables defined in a function are called local. When you declare non-pointer variables, memory is allocated using the methods stack frame. When the method is finished, the stack frame is destroyed, losing whatever value was in those local variables. Recursive methods have unique values for each stack instance.

Classes Classes are a collection of fields (variables local to a class) and methods. Classes can inherit from other classes (yes, multiple inheritance) Classes can have constructors for different parameter sets, and destructors for when they are destroyed. Classes often have a copy constructor, which clones an instance of the class into a new instance.

Classes // Foo.h class Foo{ int a, b; // a and b are private; public: Foo(int someValue, int someOtherValue); int add(); }; Default access is private. Three accesses: public (everyone can use), protected (only children classes can use), and private (only class methods can use). Access is specified with a the modifier and a colon. Classes are usually declared in a.h file, defined in a.cpp file.

Defining the class methods The following is typically in a.cpp file: // Foo.cpp #include Foo.h Foo::Foo(int someValue, int someOtherValue){ a = someValue; b = someOtherValue; } int Foo::add(){ return someValue + someOtherValue; };

Declaring an instance of a class Foo foo(1,2); // creates an instance of Foo, // calling its constructor cout << foo.add(); // reference methods and fields with. Foo *fooPointer ; // creates a pointer of type // Foo. DOES NOT create an // instance; fooPointer = new Foo(1, 2); // creates the // instance, invoking constructor, // storing the address in fooPointer fooPointer->add(); //references members with -> delete fooPointer; // releases memory, calling destructor.

Executable programs Needs exactly one main method in global scope. int main(int argCount, char * args[]){ cout << Hello world! << endl; }

Argument passing by value int doStuff(Foo foo){ // pass by value } A new instance of foo is created, calling the copy constructor if present. When the method exits, the destructor is called. Might be expensive.

Argument passing by reference int doStuff(Foo &foo){ // the & denotes pass by reference foo = bar; } Foo foo; doStuff(foo); // foo is now bar foo is not copied, no destructor or constructor is invoked. However, changes to foo are persistent beyond the scope.

Const reference Using the keyword const makes it so that you cannot make any assignments to the variable. int foo(int a, int &b, const int &c){ a = 5; // okay, but change is only for scope b = 5; // okay, changes persist beyond scope c = 5; // will not compile. } int x, y, z; x = 0, y = 0, z = 0; foo(x, y, z); cout << x = << x <<, y = << y <<, z = << z << endl;

Recursion To understand recursion, you must first understand recursion. A function that calls itself, usually using slightly adjusted parameters. Usually terminated when a base case is met. Good alternative to loops.

Recursion void recurse(){ recurse(); } This will never terminate, though.

Recursion void russianDoll(int size){ if (size == 0) return; cout << I am a Russian doll of size << size << endl; russionDoll(size – 1); }

Factorial 4! = 4 * 3 * 2 * 1 int factorial(int number){ if (number <= 1) return 1; // base case return number * factorial(number – 1); }

Fibonacci Fib 0 = 0 base case Fib 1 = 1 base case Fib n = Fib n-1 + Fib n-2 for n > 1 recursive case int Fib(int n) { if (n <= 1) { can handle both return n; base cases together } // {n > 0} an assertion return Fib(n-1) + Fib(n-2); recursive case }

Recursion Define your base case Adjust your parameters in your method call by working towards the base case.