Templated recursive linked lists

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Recursion - see Recursion. RHS – SOC 2 Recursion We know that: –We can define classes –We can define methods on classes –Mehtods can call other methods.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
Computer Science II Recursion Professor: Evan Korth New York University.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Programming with Recursion
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CS101 Computer Programming I Chapter 4 Extra Examples.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Powerful Tool
CS314 – Section 5 Recitation 9
Sections 4.1 & 4.2 Recursive Definitions,
TK1924 Program Design & Problem Solving Session 2011/2012
Recursion - see Recursion
Recursion Version 1.0.
REPETITION CONTROL STRUCTURE
Introduction to Recursion
Recursion DRILL: Please take out your notes on Recursion
Computer Science 4 Mr. Gerb
Separate Compilation and Namespaces
CS 326 Programming Languages, Concepts and Implementation
LESSON 20.
Recursion 12-Nov-18.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion - see Recursion
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Programming in Java Assertion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion 29-Dec-18.
Debugging at Scale.
When your program crashes
Chapter 6: User-Defined Functions I
Yan Shi CS/SE 2630 Lecture Notes
CS148 Introduction to Programming II
Recursion 23-Apr-19.
Recursion.
Recursion.
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
CIS 110: Introduction to Computer Programming
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Unit Testing.
Lecture 6 - Recursion.
Presentation transcript:

Templated recursive linked lists uff da But first...

Women in Computing Ice cream social Thursday at 6pm in Herak Courtyard The group is for all who are in favor of gender equality in computer science

Tutoring

Assignment 1 For your first assignment, you will be taking linked list and adapting it to be templated, and to have all functions implemented recursively This assignment is due a week from today Let's take a look at the existing code...

Assertions An assertion is a statement that must be true for your code to function correctly Include the header file <cassert> to have access to assert() assert() takes a single parameter: a boolean expression that should be true If the parameter is false, assert() will stop your program, output an error message, and trigger a debugger (if you are running with one) assert(factorial(1) == 1) a.out: main.cpp:13: int main(): Assertion `factorial(1) == 1' failed.

Using a templated class Think of templates as telling C++ a recipe to create a class based on a class you're passing as a parameter Whenever* you're referring to a templated class, you must include what type you're passing to it Let's look at a TenElementArray class...

TenElementArray code #ifndef TENELEMENTARRAY_H #define TENELEMENTARRAY_H template<class ArrayType> class TenElementArray { public:     ArrayType data[10];     void setValue(ArrayType val, int index) {         data[index] = val;     }    }; #endif

Making a class templated If you have an existing class, there's a fairly straightforward method to make it templated: Move member function bodies into the header file Choose an identifier to use as your template class parameter Replace datatypes you want to be templated with the parameter name Change places using the class to specify the template type For an example, let's look at the homework assignment writeup...

Recursion We need two things to make a recursive solution to a problem: A base case Other cases that we can solve in terms of solutions to simpler versions of the problem Let's look at the factorial function

Recursion: factorial introduction The factorial of a non-negative number n is equal to to all numbers between 1 and that number multiplied together An iterative solution to this is fairly simple Give it a shot!

Recursion: factorial via recursion We need two things to make a recursive solution to a problem: A base case Other cases that we can solve in terms of solutions to simpler versions of the problem What is our base case for factorial? What is our recursive step? (How does factorial(n) relate to the answer to a simpler version of the problem?)

Debugging recursion When you compile your code, use the -Wall and -g flags If you're on clang++ on macOS or Linux, also use -fsanitize=address If your program crashes, try running it under a debugger With gdb: gdb your-program-name gdb ./a.out With lldb: lldb your-program-name lldb ./a.out Then type r<enter> to run your program If it crashes, type bt<enter> to see what functions were running and where

Recursion: helper functions Sometimes, for the recursive call, we need more information that we need in our public-facing function In these cases, we write helper functions that perform the recursion Our publically-accessible will almost immediately return the answer as calculated by the helper function Let's look at our factorial example again...

Factorial with a helper function