Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD
|
|
|
- Jeffrey Smith
- 9 years ago
- Views:
Transcription
1 1 Slides 1. Table of Contents 2. Definitions 3. Simple 4. Recursive Execution Trace 5. Attributes 6. Recursive Array Summation 7. Recursive Array Summation Trace 8. Coding Recursively 9. Recursive Design 10. Avoiding Pitfalls 11. Middle Decomposition 12. Call Tree Traces 13. Edges & Center Decomposition 14. Recursive Sorting 15. Backtracking 16. Knapsack Solution 17. Runtime Stack 18. Knap Runtime Trace Snapshot 19. Storage Organization
2 Definitions 2 see a process in which the result of each repetition is dependent upon the result of the next repetition. Simplifies program structure at a cost of function calls Hofstadter's Law It always takes longer than you expect, even when you take into account Hofstadter's Law. Sesquipedalian a person who uses words like sesquipedalian. Yogi Berra Its déjà vu all over again.
3 Simple 3 A procedure or function which calls itself is a recursive routine. Consider the following function, which computes N! 1 * 2 *... * N int int Factorial(int Factorial(int n n int int Product Product 1, 1, 2 2 while while ( ( < < n n Product Product Product Product * * return return (Product (Product Now consider a recursive version of Factorial: int int Factorial(int Factorial(int n n if if ( ( n n > > 1 1 return( return( n n * * Factorial Factorial (n-1 (n-1 else else return(1 return(1
4 Recursive Execution Trace 4 Factorial (5 return and then the return sequence 5 * Factorial (4 return 24 4 * Factorial (3 return 6 3 * Factorial (2 return 2 First the recursive descent... 2 * Factorial (1 1 return 1
5 Attributes 5 Every recursive algorithm can be implemented nonrecursively. recursion <> iteration Eventually, the routine must not call itself, allowing the code to "back out". Recursive routines that call themselves continuously are termed: infinite recursion <> infinite loop Problem with this recursive factorial implementation? Negative numbers! is inefficient at runtime.
6 Recursive Array Summation 6 Here is a recursive function that takes an array of integers and computes the sum of the elements: // X[] array of integers to be summed // Start start summing at this index... // Stop... and stop summing at this index // int SumArray(const int X[], int Start, int Stop // error check if (Start > Stop Start < 0 Stop < 0 return 0 else if (Start Stop // base case return X[Stop] else // recursion return (X[Start] + SumArray(X, Start + 1, Stop
7 Recursive Array Summation Trace 7 The call: const int Size 5 int X[Size] 37, 14, 22, 42, 19 SumArray(X,0,Size- 1// note Stop is last valid index would result in the recursive trace: // return values: SumArray(X, 0, 4 // 134 return(x[0]+sumarray(x,1,4 // return(x[1]+sumarray(x,2,4 // return(x[2]+sumarray(x,3,4 // return(x[3]+sumarray(x,4,4 // return X[4] // 19
8 Coding Recursively 8 Mathematical Induction Model Solve the trivial "base" case(s. Restate general case in 'simpler' or 'smaller' terms of itself. Array Sum Example Determine the size of a single linked list. Base Case : array size 1, sum the element General Case : first element + Sum(Rest of Array // X[] array of integers to be summed // Start start summing at this index... // Stop... and stop summing at this index // int SumArray(const int X[], int Start, int Stop // error check if (Start > Stop Start < 0 Stop < 0 return 0 else if (Start Stop // base case return X[Stop] else // recursion return (X[Start] + SumArray(X, Start + 1, Stop Example Example of of tail tail recursion recursion (going (going up up recursion. recursion. Tail Tail recursive recursive functions functions are are characterized characterized by by the the recursive recursive call call being being the the last last statement statement in in the the function, function, (can (can easily easily be be replaced replaced by by a a loop. loop.
9 Recursive Design 9 Problem: Code a function void intcomma( long that outputs the integer argument comma separated : e.g., the call: intcomma ( displays: 123,456,789 Top-Down Design Code void intcomma ( long num if (num is less than 1000 display num else display comma separated digits above 1000 display comma display digits below 1000 void intcomma ( long num if (num < 1000 cout << setw(3 << num else intcomma(num / 1000 cout << ',' << setw(3 << num % 1000 Consider: Consider: intcomma( intcomma( intcomma( intcomma(
10 Avoiding Pitfalls General Solution void intcomma ( long num 10 if (num < 0 cout << '-' num -num if (num < 1000 cout << setw(3 << num else intcomma(num / 1000 cout << ',' num num % 1000 cout << (num / 100 num num % 100 cout << (num / 10 << (num % 10 Trace intcomma( intcomma( intcomma( and intcomma(9087 and intcomma(9 and intcomma(9 9 // display sign for negatives Example Example of of going going down down (head (head recursion recursion // display digits // separately // for zeroes output alternatively string prefix (num < 10? "00" : (num < 100? "0" : "" cout << prefix << num 9,087 9,087,605 9,087,605,430
11 Middle Decomposition 11 Problem: Given an array of integers of n+1 elements code a function to return the index of the maximum value in the array. Solution: Check if the middle element is the largest if so return its index otherwise return the index of either the largest element in the lower half or the largest element in the upper half, whichever is the larger of the two. int rmax(const int ray[], int start, int end const int Unknown -1 Example Example of of int mid, h1max, h2max splitting splitting into if (end < start return Unknown into halves halves recursion recursion mid (start + end / 2 h1max rmax(ray, start, mid-1//left half if (h1max Unknown h1max start h2max rmax(ray, mid+1, end //right half if (h2max Unknown h2max end if ( (ray[mid] > ray[h1max] && (ray[mid] > ray[h2max] return mid else return( (ray[h1max] > ray[h2max]? h1max : h2max Unknown checks ensure that indices are within array subscript range
12 Call Tree Traces Given: 12 ray [0] [1] [2] [3] [4] Call Tree Trace of h1max0 0 rmax(ray, 0, 4 mid2 h2max4 4 4 h1max0 unknown rmax(ray, 0, 1 mid0 rmax(ray, 0, -1 h1max1 unknown h2max1 1 rmax(ray, 1, 1 mid1 h2max1 unknown h1max3 unknown rmax(ray, 3, 2 rmax(ray, 3, 4 mid3 4 h1max4 unknown h2max4 h2max 4 rmax(ray, 4, 4 mid4 unknown rmax(ray, 1, 0 rmax(ray, 2, 1 rmax(ray, 4, 3 rmax(ray, 5, 4 Middle Middle decomposition decomposition (splitting (splitting problem problem into into halves, halves, recursive recursive functions functions are are best best traced traced with with tree tree diagrams diagrams
13 Edges & Center Decomposition 13 Problem: sort a subset, (m:n, of an array of integers (ascending order Solution: Find the smallest and largest values in the subset of the array (m:n and swap the smallest with the m th element and swap the largest with the n th element, (i.e. order the edges. Sort the center of the array (m+1: n-1. Solution Trace unsorted array m [0] [1] [2] [3] [4] [5] [6] [7] [8] n [9] after call#1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] after call#3 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Variation Variation of of the the selection selection sort sort algorithm algorithm
14 Recursive Sorting 14 void duplexselection(int ray[], int start, int end int mini start, maxi end if (start < end //startend > 1 element to sort findminimaxi( ray, start, end, mini, maxi swapedges(ray, start, end, mini, maxi duplexselection( ray, start+1, end-1 void findminimaxi( const int ray[], int start, int end, int& mini, int& maxi if (start < end //subset to search exists if (ray[start] < ray[mini] mini start else if (ray[start] > ray[maxi] maxi start findminimaxi( ray, start+1, end, mini, maxi void swapedges(int ray[], int start, int end, int mini, int maxi //check for swap interference if ( (mini end && (maxi start swap( ray[start], ray[end] //check for low 1/2 interference else if (maxi start swap( ray[maxi], ray[end] swap( ray[mini], ray[start] // (mini end no interference else swap( ray[mini], ray[start] void void swap swap swap(ray[maxi], ray[end] ( ( int& int& x, x, int& int& y y int int tmp tmp x x x x y y y y tmp tmp
15 Backtracking 15 Knapsack Problem (very weak form Given an integer total, and an integer array, determine if any collection of array elements within a subset of the array sum up to total. Assume the array contains only positive integers. Special Base Cases total 0 : solution: the collection of no elements adds up to 0. total < 0 : solution: no collection adds to sum. start of subset index > end of subset index : solution: no such collection can exist. Inductive Step Check if a collection exists containing the first subset element. Does a collection exist for total - ray[ subset start ] from subset start + 1 to end of subset? If no collection exists containing ray[ subset start ] check for a collection for total from subset start + 1 to the end of the subset. Backtracking Backtracking step. step. Function Function searches searches for for alternative alternative solution solution undoing undoing previous previous possible possible solution solution search search work. work.
16 Knapsack Solution Knap backtracking function 16 bool Knap (const int ray[], int total, int start, int end if (total 0 // empty collection adds up to 0 return true if ( (total < 0 (start > end //no such return false //collection exists //check for collection containing ray[start] if (Knap(ray, total-ray[start], start+1, end return true // check for collection w/o ray[start] return (Knap(ray, total, start+1, end Trace Knap(ray, 100, 0, 4 TRUE Knap(ray, 50, 1, 4 TRUE Knap(ray, 30, 2, 4 ray [0] [1] [2] [3] [4] FALSE TRUE Knap(ray, -10, 3, 4 Knap(ray, 30, 3, 4 FALSE TRUE Knap(ray, -30, 4, 4 Knap(ray, 30, 4, 4 Backtracking Backtracking steps steps TRUE Knap(ray, 0, 5, 4
17 Runtime Stack 17 Underpinnings Every instance of a function execution (call creates an Activation Record, (frame for the function. Activation records hold required execution information for functions: Return value for the function Pointer to activation record of calling function Return memory address, (calling instruction address Parameter storage Local variable storage Runtime Stack Activation records are created and stored in an area of memory termed the runtime stack. Fny( activation record Fnx( activation record main( activation record local storage Parameter storage Return address Pointer previous act. rec. Fn return value local storage Parameter storage Return address Pointer previous act. rec. Fn return value local storage Parameter storage Return address Pointer previous act. rec. Fn return value
18 Knap Runtime Trace Snapshot 18 First backtrack step(during fourth call Let first recursive call in knap be at address α Let second recursive call in knap be at address β Activation record destroyed as third recursive call completes execution second recursive call first recursive call external call ray α? ray α? ray α? ray Ψ? local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value
19 Storage Organization 19 Typical C++ program execution memory model System privileges (not accessible to the C program Binary Code (text segment Lowest memory addresss Static Data (data segment Runtime Stack Function activation record management Dynamic memory structure management Storage Corruption Heap Highest memory addresss Infinite regression results in a collision between the runtime stack & heap termed a run-time stack overflow error. Illegal pointer de-references (garbage, dangling-references often result in memory references outside the operating system allocated partition, (segment for the C program resulting in a segmentation error (GPF - access violation and core dump.
Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)
Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the
The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!
The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >
Section IV.1: Recursive Algorithms and Recursion Trees
Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller
16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution
16. Recursion COMP 110 Prasun Dewan 1 Loops are one mechanism for making a program execute a statement a variable number of times. Recursion offers an alternative mechanism, considered by many to be more
What Is Recursion? Recursion. Binary search example postponed to end of lecture
Recursion Binary search example postponed to end of lecture What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion
Analysis of Binary Search algorithm and Selection Sort algorithm
Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:
Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)
What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...
What is a Loop? CSC Intermediate Programming Looping A loop is a repetition control structure It causes a single statement or a group of statements to be executed repeatedly It uses a condition to control
Chapter 5. Recursion. Data Structures and Algorithms in Java
Chapter 5 Recursion Data Structures and Algorithms in Java Objectives Discuss the following topics: Recursive Definitions Method Calls and Recursion Implementation Anatomy of a Recursive Call Tail Recursion
Lecture Notes on Linear Search
Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is
recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1
topics: recursion searching cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1 recursion recursion is defining something in terms of itself there are many examples in nature and
Stacks. Linear data structures
Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++
Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A
Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }
Functions Recursion C++ functions Declare/prototype int myfunction (int ); Define int myfunction (int x){ int y = x*x; return y; Call int a; a = myfunction (7); function call flow types type of function
Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.
Software Testing Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program. Testing can only reveal the presence of errors and not the
CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++
Brad Rippe CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++ Recursion Recursion CHAPTER 14 Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively
10CS35: Data Structures Using C
CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a
Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.
Recursion Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive. Why recursion? o For many problems, the recursion solution is more natural than the alternative
csci 210: Data Structures Recursion
csci 210: Data Structures Recursion Summary Topics recursion overview simple examples Sierpinski gasket Hanoi towers Blob check READING: GT textbook chapter 3.5 Recursion In general, a method of defining
Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1
Recursion Slides by Christopher M Bourke Instructor: Berthe Y Choueiry Fall 007 Computer Science & Engineering 35 Introduction to Discrete Mathematics Sections 71-7 of Rosen cse35@cseunledu Recursive Algorithms
PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?
1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members
6. Standard Algorithms
6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary
Sample Questions Csci 1112 A. Bellaachia
Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k
Data Structures, Practice Homework 3, with Solutions (not to be handed in)
Data Structures, Practice Homework 3, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 9, Exercise 1: What is the order of each of the following tasks in the worst case? (a) Computing
Data Structure with C
Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which
Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge
Common Data Structures
Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees
PES Institute of Technology-BSC QUESTION BANK
PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions
CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting
CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)
IS0020 Program Design and Software Tools Midterm, Feb 24, 2004. Instruction
IS0020 Program Design and Software Tools Midterm, Feb 24, 2004 Name: Instruction There are two parts in this test. The first part contains 50 questions worth 80 points. The second part constitutes 20 points
While Loop. 6. Iteration
While Loop 1 Loop - a control structure that causes a set of statements to be executed repeatedly, (reiterated). While statement - most versatile type of loop in C++ false while boolean expression true
Pseudo code Tutorial and Exercises Teacher s Version
Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy
Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;
50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root)
Recursion and Recursive Backtracking
Recursion and Recursive Backtracking Computer Science E-119 Harvard Extension School Fall 01 David G. Sullivan, Ph.D. Iteration When we encounter a problem that requires repetition, we often use iteration
Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1
Definition Course: Programming II - Abstract Data Types The ADT Stack The ADT Stack is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit insertions
C++FA 5.1 PRACTICE MID-TERM EXAM
C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer
Sources: On the Web: Slides will be available on:
C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,
Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Example Assume the following specification: Input: read a number N > 0 Output:
Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:
Example Introduction to Programming (in C++) Loops Assume the following specification: Input: read a number N > 0 Output: write the sequence 1 2 3 N (one number per line) Jordi Cortadella, Ricard Gavaldà,
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential
1 Description of The Simpletron
Simulating The Simpletron Computer 50 points 1 Description of The Simpletron In this assignment you will write a program to simulate a fictional computer that we will call the Simpletron. As its name implies
Moving from CS 61A Scheme to CS 61B Java
Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you
Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010
Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement
Mathematical Induction. Lecture 10-11
Mathematical Induction Lecture 10-11 Menu Mathematical Induction Strong Induction Recursive Definitions Structural Induction Climbing an Infinite Ladder Suppose we have an infinite ladder: 1. We can reach
Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap
Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap Singly linked list (1) Data about exam results are stored into a singly linked list. Each list
Moving from C++ to VBA
Introduction College of Engineering and Computer Science Mechanical Engineering Department Mechanical Engineering 309 Numerical Analysis of Engineering Systems Fall 2014 Number: 15237 Instructor: Larry
Computer Science 210: Data Structures. Searching
Computer Science 210: Data Structures Searching Searching Given a sequence of elements, and a target element, find whether the target occurs in the sequence Variations: find first occurence; find all occurences
50 Computer Science MI-SG-FLD050-02
50 Computer Science MI-SG-FLD050-02 TABLE OF CONTENTS PART 1: General Information About the MTTC Program and Test Preparation OVERVIEW OF THE TESTING PROGRAM... 1-1 Contact Information Test Development
Converting a Number from Decimal to Binary
Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive
Binary search algorithm
Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than
Answers to Review Questions Chapter 7
Answers to Review Questions Chapter 7 1. The size declarator is used in a definition of an array to indicate the number of elements the array will have. A subscript is used to access a specific element
DATA STRUCTURES USING C
DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give
6.2 Permutations continued
6.2 Permutations continued Theorem A permutation on a finite set A is either a cycle or can be expressed as a product (composition of disjoint cycles. Proof is by (strong induction on the number, r, of
6. Control Structures
- 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,
COMPUTER SCIENCE. Paper 1 (THEORY)
COMPUTER SCIENCE Paper 1 (THEORY) (Three hours) Maximum Marks: 70 (Candidates are allowed additional 15 minutes for only reading the paper. They must NOT start writing during this time) -----------------------------------------------------------------------------------------------------------------------
Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c
Lecture 3 Data structures arrays structs C strings: array of chars Arrays as parameters to functions Multiple subscripted arrays Structs as parameters to functions Default arguments Inline functions Redirection
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.
Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to
COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012
Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about
Chapter 5 Functions. Introducing Functions
Chapter 5 Functions 1 Introducing Functions A function is a collection of statements that are grouped together to perform an operation Define a function Invoke a funciton return value type method name
Binary Heap Algorithms
CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks [email protected] 2005 2009 Glenn G. Chappell
Functional Programming in C++11
Functional Programming in C++11 science + computing ag IT-Dienstleistungen und Software für anspruchsvolle Rechnernetze Tübingen München Berlin Düsseldorf An Overview Programming in a functional style
Introduction to data structures
Notes 2: Introduction to data structures 2.1 Recursion 2.1.1 Recursive functions Recursion is a central concept in computation in which the solution of a problem depends on the solution of smaller copies
For the next three questions, consider the class declaration: Member function implementations put inline to save space.
Instructions: This homework assignment focuses on basic facts regarding classes in C++. Submit your answers via the Curator System as OQ4. For the next three questions, consider the class declaration:
12-6 Write a recursive definition of a valid Java identifier (see chapter 2).
CHAPTER 12 Recursion Recursion is a powerful programming technique that is often difficult for students to understand. The challenge is explaining recursion in a way that is already natural to the student.
Module 2 Stacks and Queues: Abstract Data Types
Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which
Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays
Motivation Suppose that we want a program that can read in a list of numbers and sort that list, or nd the largest value in that list. To be concrete about it, suppose we have 15 numbers to read in from
Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s
Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number
Regression Verification: Status Report
Regression Verification: Status Report Presentation by Dennis Felsing within the Projektgruppe Formale Methoden der Softwareentwicklung 2013-12-11 1/22 Introduction How to prevent regressions in software
Lecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure
Passing 1D arrays to functions.
Passing 1D arrays to functions. In C++ arrays can only be reference parameters. It is not possible to pass an array by value. Therefore, the ampersand (&) is omitted. What is actually passed to the function,
Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example
Operator Overloading Lecture 8 Operator Overloading C++ feature that allows implementer-defined classes to specify class-specific function for operators Benefits allows classes to provide natural semantics
PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON
PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London
Algorithm Design and Recursion
Chapter 13 Algorithm Design and Recursion Objectives To understand basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms for linear and binary
You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:
Lecture 7 Picking Balls From an Urn The problem: An urn has n (n = 10) balls numbered from 0 to 9 A ball is selected at random, its' is number noted, it is set aside, and another ball is selected from
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation
Lecture 12 Doubly Linked Lists (with Recursion)
Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively)
9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements
9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending
What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara
CMPSC 24: Lecture 13 Recursion Divyakant Agrawal Department of Computer Science UC Santa Barbara 5/12/10 1 Lecture Plan Recursion General structure of recursive soluions Why do recursive soluions terminate?
I PUC - Computer Science. Practical s Syllabus. Contents
I PUC - Computer Science Practical s Syllabus Contents Topics 1 Overview Of a Computer 1.1 Introduction 1.2 Functional Components of a computer (Working of each unit) 1.3 Evolution Of Computers 1.4 Generations
Solving Problems Recursively
Solving Problems Recursively Recursion is an indispensable tool in a programmer s toolkit Allows many complex problems to be solved simply Elegance and understanding in code often leads to better programs:
ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION 2008-10-23/5:15-6:45 REC-200, EVI-350, RCH-106, HH-139
ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION 2008-10-23/5:15-6:45 REC-200, EVI-350, RCH-106, HH-139 Instructions: No aides. Turn off all electronic media and store them under your desk. If
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C
Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection
Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.
Organization of Programming Languages CS320/520N Razvan C. Bunescu School of Electrical Engineering and Computer Science [email protected] Names, Bindings, and Scopes A name is a symbolic identifier used
CS 2412 Data Structures. Chapter 2 Stacks and recursion
CS 2412 Data Structures Chapter 2 Stacks and recursion 1 2.1 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one end, called top of the stack. Examples:
Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015
Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may
Data Structures. Algorithm Performance and Big O Analysis
Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined
(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.
3130CIT: Theory of Computation Turing machines and undecidability (IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems. An undecidable problem
MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------
=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com
C++FA 3.1 OPTIMIZING C++
C++FA 3.1 OPTIMIZING C++ Ben Van Vliet Measuring Performance Performance can be measured and judged in different ways execution time, memory usage, error count, ease of use and trade offs usually have
Curriculum Map. Discipline: Computer Science Course: C++
Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code
Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction
Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know
Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Sorting Let elem be a type with a operation, which is a total order A vector
Keil C51 Cross Compiler
Keil C51 Cross Compiler ANSI C Compiler Generates fast compact code for the 8051 and it s derivatives Advantages of C over Assembler Do not need to know the microcontroller instruction set Register allocation
Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited
Hash Tables Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited We ve considered several data structures that allow us to store and search for data
13 Classes & Objects with Constructors/Destructors
13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.
