CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)
|
|
- Gwenda Holland
- 6 years ago
- Views:
Transcription
1 CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x); else return x; Trace the above function for the following calls: what (104,16) what (15,35) what (63,18) What is the objective of the above function? X=104 X=88 what (104,16) X=72 X=56 X=40 X=24 X=8 y=8 X=8 what (15,35) X=15 y=35 X=15 y=20 X=15 y=5 X=10 y=5 X=5 y=5 what (63,18) X=63 X=45 X=27 X=9 X=9 y=9 The function finds the GCD for two integers This is the recursive algorithm for the GCD (Greatest common Divisor of two integers). Write a recursive Boolean function palindrome ( s, first, last ) to return true if the string s is a palindrome, that is, the string reads the same backwards as forwards. Examples are dad and bob. The parameters first, last are the indices of the first and last elements of the part of the string being checked, respectively. Validate your function using examples of your choice. bool palindrome(string s, int first, int last) if (first>=last) return true; else if(s[first]!= s[last]) return false; else return palindrome(s, first1, last-1); dad true Tracing Palindrome dad and daa true F=0, L=2 F=1, L=1 dabd false F=0, L=3 false F=1, L=2
2 Given the following array a[ ] of integers: Trace the function call m = process (a, 12, 10 ) to determine the value of m and the final contents of a[ ] returned by the following recursive function: int process (int a[ ], int n, int x ) if ((x < 0) (x > n-1)) return 0; else if (a[x] == 0) return 0; else a[x] = 0; return (1 process(a, n, x-1) process(a, n, x1)); m = process (a, 12, 10 ); X=8 A[8] = 0 X=7 X=9 0 0 X=9 A[9] = 0 0 n=12, x=10 A[10] = 0 X=11 A[11] = X=10 X=10 X=12 m = 4 This algorithm will return the size of the non-zero sequence of elements of which the element at x is a member. It will replace these elements by zeros. Hence, the size of the non-zero sequence of elements of which element at location 10 is a member will be 4 (values 1, 2, 3, 7). The final array will be:
3 Consider the following recursive function : long int Func ( int n, int m ) if ((m == 0) (m == n)) return 1 ; else return ( Func ( n-1, m) Func( n-1, m-1) ) ; If long int z = Func( 5, 3 ); trace the above function to determine the value of z. long int z = Func( 5, 3 ); z=10 4 n=5, m=3 6 n=4, m=3 n=4, m= n=3, m=3 n=3, m=2 n=3, m=2 n=3, m= n=2, m=2 n=2, m=1 n=2, m=2 n=2, m=1 n=2, m=1 n=2, m= ,1 1,0 1,1 1,0 1,1 1,0 This is the recursive algorithm for finding the number of combinations of n objects taken m at a time (Binomial Coefficient). Consider an array a[ ] of non-zero integers. Implement a recursive function sum (a[ ], s, e) based on the Divide & Conquer method to return the sum of all the elements in the array between index (s) and index (e) inclusive (use division at the approximate middle). Test your function by tracing it for the call sum (a, 0, 6) given the following array:
4 int sum ( int a[ ], int s, int e) if ( s > e ) return 0; else if ( s == e ) return a [ s ]; else int m = ( s e ) / 2; return sum ( a, s, m ) sum ( a, m1, e ); Tracing sum (a, 0, 6): result is the sum of elements in array = 29 sum (Divide & Conquer) 15 s=0, e=6 M= s=0, e=3 M=1 s=4, e=6 M=5 A[0] s=0,e=0 6 s=0, e=1 M=0 A[1] A[2] 9 s=2, e=3 M=2 A[3] A[4] s=4, e=5 M=4 12 A[6] A[5] s=1,e=1 s=2,e=2 s=3,e=3 s=4,e=4 s=5,e=5 s=6, e=6 Consider the following main function that invokes the recursive function value. Trace the function call to determine the final contents of the array a[ ]. int main( ) int a[6] = 5, 7, 12, 3, 14, 3 ; int k = 1; for ( int i = 0; i < 6; i) a[ i ] = value (k) / (k*k); k *= 2; int value ( int n ) if ( n < 2 ) return 0; else return ( 4 * value (n / 2) n * n );
5 Answer: i k value(k) a[i] Write a recursive function oddcount ( int a[ ], int s, int e) to receive an integer array a [ ] a start index (s), and an end index (e) and return the number of odd integers in that array between s and e. int oddcount(int a[ ], int s, int e) if (s > e) return 0; else return ( a[s]%2 ) oddcount (a, s1, e); Write a recursive function that receives a string of characters and returns the accumulating sum of the ASCII values of the characters in the string, excluding blanks from the sum. //Blanks Included int asciisum (string s, int first, int last) if( first > last) return 0; return int ( s [first] ) asciisum (s, first 1, last); // Blanks Excluded int asciisum (string s, int first, int last) if( first > last) return 0; if( s[first] == ) return asciisum (s, first 1, last); return int ( s [first] ) asciisum (s, first 1, last);
6 Write a recursive function to return the number of zeros in an array A of integers of size n Write a recursive boolean function to return true if an integer x is found in the first n elements of an array of integers A, and false otherwise. //Recursive Sequential Search of x in array A from location s through location e. bool LinSearch (int A[ ], int x, int s, int e) if (x == A[s]) return true; else if (s == e) return false; else return LinSearch (A,x,s1,e); Call this function as: LinSearch(A, x, 0, n-1); Write a recursive function Sum(int X[ ], int N) to return the sum of the values in an integer array X with subscripts from 1 to N. Let x be positive real. To calculate the square root of x by Newton's Method, we start with an initial approximation a = x/2. If abs (a*a-x) < epsilon, we stop and return with the result (a). Otherwise, we replace (a) by (a x/a) / 2 and repeat the process until we find an approximation close enough to stop. Write a recursive function Newton (x, a, epsilon) to compute the square root of x using the above method and taking epsilon = Write a function Square_Root(x) to call the above function and return the final result of the square root of x. Assume that an array a[ ] of characters of size N = 7 has the following contents: O N E T W O Trace the following functions to show the output of the calls Show1(a, 0, N-1) and Show2(a, 0, N-1) void Show1 ( char a[ ], int s, int e) if (s < = e) int m = (s e)/2; cout << a[m]; Show1 ( a, s, m-1); Show1 ( a, m1, e); void Show2 ( char a[ ], int s, int e) if (s < = e) int m = (s e)/2; Show2 ( a, s, m-1); cout << a[m]; Show2 ( a, m1, e);
CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion
CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x);
More informationCSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid
CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid 1. Definition: The expression Recursion is derived from Latin: Re- = back and currere = to run, or to happen again, especially at repeated
More informationCSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare
More informationNumber System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value
1 Number System Introduction In this chapter, we will study about the number system and number line. We will also learn about the four fundamental operations on whole numbers and their properties. Natural
More informationI SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.
I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK 70. a) What is the difference between Hardware and Software? Give one example for each. b) Give two differences between primary and secondary memory.
More informationCSCE 110: Programming I
CSCE 110: Programming I Sample Questions for Exam #1 February 17, 2013 Below are sample questions to help you prepare for Exam #1. Make sure you can solve all of these problems by hand. For most of the
More informationCOMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More information3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)
Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.
More information1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?
Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include b) What using statement do you always put at the top of
More informationECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm
ECE G205 Fundamentals of Computer Engineering Fall 2003 Exercises in Preparation to the Midterm The following problems can be solved by either providing the pseudo-codes of the required algorithms or the
More informationCSCE 210 Dr. Amr Goneid Exercises (1): Stacks, Queues. Stacks
CSCE 210 Dr. Amr Goneid Exercises (1): Stacks, Queues Stacks Given an input sequence of integers 1, 2, 3, 4, 5, 6 and only three operations on a stack: 1. C: Copy next input directly to output list. 2.
More informationConditionals !
Conditionals 02-201! Computing GCD GCD Problem: Compute the greatest common divisor of two integers. Input: Two integers a and b. Output: The greatest common divisor of a and b. Exercise: Design an algorithm
More informationUEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter
UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with
More informationUniversity of Dublin
University of Dublin TRINITY COLLEGE Faculty of Enginering & Systems Sciences School of Engineering Junior Freshman Engineering Trinity Term 2015 Computer Engineering I (1E3) Date Location Time Dr L. Hederman
More informationThe American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science
The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Khalil Final Exam Fall 2013 Last Name :... ID:... First Name:... Form
More informationModule Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1
UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2015/16 INTRODUCTORY PROGRAMMING CMP-0005B Time allowed: 2 hours. Answer BOTH questions from section A and ONE question
More informationMathematical Induction
Mathematical Induction Victor Adamchik Fall of 2005 Lecture 3 (out of three) Plan 1. Recursive Definitions 2. Recursively Defined Sets 3. Program Correctness Recursive Definitions Sometimes it is easier
More informationIntroduction to Computer Programming, Spring Term 2018 Practice Assignment 6 Discussion:
German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Mohammed Abdel Megeed Introduction to Computer Programming, Spring Term 2018 Practice Assignment 6 Discussion:
More informationLab Manual. Program Design and File Structures (P): IT-219
Lab Manual Program Design and File Structures (P): IT-219 Lab Instructions Several practicals / programs? Whether an experiment contains one or several practicals /programs One practical / program Lab
More informationWeek 1 Questions Question Options Answer & Explanation A. 10 B. 20 C. 21 D. 11. A. 97 B. 98 C. 99 D. a
Sr. no. Week 1 Questions Question Options Answer & Explanation 1 Find the output: int x=10; int y; y=x++; printf("%d",x); A. 10 B. 20 C. 21 D. 11 Answer: D x++ increments the value to 11. So printf statement
More informationObject Oriented Programming Using C++ Mathematics & Computing IET, Katunayake
Assigning Values // Example 2.3(Mathematical operations in C++) float a; cout > a; cout
More informationLab Instructor : Jean Lai
Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.
More informationStandard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing
Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion Copyright 2003 Scott/Jones Publishing Topics 19.1 Introduction to Recursion 19.2 The Recursive Factorial Function 19.3 The Recursive
More informationCSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists
CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists Exercises on Structs (Solutions) (a) Define a struct data type location with integer members row, column Define another
More informationCOT 3100 Spring 2010 Midterm 2
COT 3100 Spring 2010 Midterm 2 For the first two questions #1 and #2, do ONLY ONE of them. If you do both, only question #1 will be graded. 1. (20 pts) Give iterative and recursive algorithms for finding
More informationTotal 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.
The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Practice Sheet #06 Topic: Recursion in C 1. What string does the following program print? #include #include
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationWhat is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:
Recursion What is recursion? Week 10! Generally, when something contains a reference to itself Gaddis:19.1-19.5! Math: defining a function in terms of itself CS 5301 Spring 2015 Jill Seaman 1! Computer
More informationCOMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1
COMP 202 Recursion CONTENTS: Recursion COMP 202 - Recursion 1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself COMP 202 - Recursion
More informationSection A Arithmetic ( 5) Exercise A
Section A Arithmetic In the non-calculator section of the examination there might be times when you need to work with quite awkward numbers quickly and accurately. In particular you must be very familiar
More informationOther operators. Some times a simple comparison is not enough to determine if our criteria has been met.
Lecture 6 Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name
More informationWhat is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:
Recursion What is recursion? Week 7! Generally, when something contains a reference to itself Gaddis:19.1-19.4! Math: defining a function in terms of itself CS 5301 Fall 2013 Jill Seaman 1! Computer science:
More informationExam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.
Name: Section: INSTRUCTIONS: (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN. (2) The total for the exam is 100 points (3) There are 8 pages with 32 problem; 15 multiple-choice, 15
More informationIntro to Computational Programming in C Engineering For Kids!
CONTROL STRUCTURES CONDITIONAL EXPRESSIONS Take the construction like this: Simple example: if (conditional expression) statement(s) we do if the condition is true statement(s) we do if the condition is
More informationMathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015
Mathematics Jaehyun Park CS 97SI Stanford University June 29, 2015 Outline Algebra Number Theory Combinatorics Geometry Algebra 2 Sum of Powers n k=1 k 3 k 2 = 1 n(n + 1)(2n + 1) 6 = ( k ) 2 = ( 1 2 n(n
More informationPROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions
PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}
More informationCS 3410 Ch 7 Recursion
CS 3410 Ch 7 Recursion Sections Pages 7.1-7.4, 7.7 93-319, 333-336 7.1 Introduction 1. A recursive method is a method that either directly or indirectly makes a call to itself. [Weiss]. It does this by
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library
More informationLOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).
LOOPS 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ). 2-Give the result of the following program: #include
More informationTwo Approaches to Algorithms An Example (1) Iteration (2) Recursion
2. Recursion Algorithm Two Approaches to Algorithms (1) Iteration It exploits while-loop, for-loop, repeat-until etc. Classical, conventional, and general approach (2) Recursion Self-function call It exploits
More informationSolutions to Assessment
Solutions to Assessment [1] What does the code segment below print? int fun(int x) ++x; int main() int x = 1; fun(x); printf( %d, x); return 0; Answer : 1. The argument to the function is passed by value.
More informationModule Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1
UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2014/15 INTRODUCTORY PROGRAMMING CMP-0005B Time allowed: 2 hours. Answer BOTH questions from section A and ONE question
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,
More informationNames and Functions. Chapter 2
Chapter 2 Names and Functions So far we have built only tiny toy programs. To build bigger ones, we need to be able to name things so as to refer to them later. We also need to write expressions whose
More informationCS205: Scalable Software Systems
CS205: Scalable Software Systems Lecture 3 September 5, 2016 Lecture 3 CS205: Scalable Software Systems September 5, 2016 1 / 19 Table of contents 1 Quick Recap 2 Type of recursive solutions 3 Translating
More informationLearning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution
Recursion [ Why is it important?] ~7 easy marks in Exam Paper Seemingly Different Coding Approach In Fact: Strengthen Top-down Thinking Get Mature in - Setting parameters - Function calls - return + work
More informationPointers, References and Arrays
Dr. Roxana Dumitrescu Department of Mathematics King s College London email: roxana.dumitrescu@kcl.ac.uk Exercises C++ SHEET 2 Pointers, References and Arrays Problem 1. Declare int variables x and y and
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More information15. Pointers, Algorithms, Iterators and Containers II
498 Recall: Pointers running over the Array 499 Beispiel 15. Pointers, Algorithms, Iterators and Containers II int a[5] = 3, 4, 6, 1, 2; for (int p = a; p < a+5; ++p) std::cout
More informationALGORITHM 2-1 Solution for Exercise 4
Chapter 2 Recursion Exercises 1. a. 3 * 4 = 12 b. (2 * (2 * fun1(0) + 7) + 7) = (2 * (2 * (3 * 0) + 7) + 7) = 21 c. (2 * (2 * fun1(2) + 7) + 7) = (2 * (2 * (3 * 2) + 7) + 7) = 45 2. a. 3 b. (fun2(2, 6)
More informationName: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer
[Closed book exam] There are 7 questions leading up to 100 points. Max alloted time: 1 hour Problem 1 (2x10=20 points). Fill in the blanks in terms of the big-theta (Θ) notation to show the asymptotic
More informationIosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C
STATEMENTS in C 1. Overview The learning objective of this lab is: To understand and proper use statements of C/C++ language, both the simple and structured ones: the expression statement, the empty statement,
More informationStructured programming
Exercises 8 Version 1.0, 1 December, 2016 Table of Contents 1. Recursion................................................................... 1 1.1. Problem 1...............................................................
More informationModule 7 Highlights. Mastered Reviewed. Sections ,
Sections 5.3 5.6, 6.1 6.6 Module 7 Highlights Andrea Hendricks Math 0098 Pre-college Algebra Topics Degree & leading coeff. of a univariate polynomial (5.3, Obj. 1) Simplifying a sum/diff. of two univariate
More informationComp 182 Data Structures Sample Midterm Examination
Comp 182 Data Structures Sample Midterm Examination 5 November 2014 Name: ANSWERS C. R. Putnam 5 November 2014 1. Design an ADT that represents a simple Social Networking Site. It should allow a user to
More informationBEng (Hons) Telecommunications. Examinations for / Semester 2
BEng (Hons) Telecommunications Cohort: BTEL/14B/FT Examinations for 2014-2015 / Semester 2 MODULE: NUMBERS, LOGICS AND GRAPHS THEORIES MODULE CODE: Duration: 3 Hours Instructions to Candidates: 1 Answer
More informationx= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!
HighPower large integer calculator intended to investigate the properties of large numbers such as large exponentials and factorials. This application is written in Delphi 7 and can be easily ported to
More informationLaboratorio di Algoritmi e Strutture Dati
Laboratorio di Algoritmi e Strutture Dati Guido Fiorino guido.fiorino@unimib.it aa 2013-2014 Maximum in a sequence Given a sequence A 1,..., A N find the maximum. The maximum is in the first or in the
More informationLecture 6: Recursion RECURSION
Lecture 6: Recursion RECURSION You are now Java experts! 2 This was almost all the Java that we will teach you in this course Will see a few last things in the remainder of class Now will begin focusing
More informationQuestion 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:
Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels: size(400, 300); b) After the above command is carried out,
More informationCOP 4516: Math for Programming Contest Notes
COP 4516: Math for Programming Contest Notes Euclid's Algorithm Euclid's Algorithm is the efficient way to determine the greatest common divisor between two integers. Given two positive integers a and
More informationRecursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?
Recursion Chapter 8 CS 3358 Summer I 2012 Jill Seaman What is recursion? Generally, when something contains a reference to itself Math: defining a function in terms of itself Computer science: when a function
More informationThe American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science
The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Howaida Ismail Final Exam Spring 2013 Last Name :... ID:... First Name:...
More informationCSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions
CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions User Defined Functions In previous labs, you've encountered useful functions, such as sqrt() and pow(), that were created by other
More informationWeek 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.
Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where
More informationCSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output
CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed
More informationSol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;
r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationRecursion. Example R1
Recursion Certain computer problems are solved by repeating the execution of one or more statements a certain number of times. So far, we have implemented the repetition of one or more statements by using
More informationC212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.
C212 Early Evaluation Exam Mon Feb 10 2014 Name: Please provide brief (common sense) justifications with your answers below. 1. What is the type (and value) of this expression: 5 * (7 + 4 / 2) 2. What
More informationRecursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.
Recursion Chapter 8 CS 3358 Summer II 2013 Jill Seaman What is recursion?! Generally, when something contains a reference to itself! Math: defining a function in terms of itself! Computer science: when
More informationName :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING
Name :. Roll No. :..... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/2012 2012 BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in
More informationRecursion. Recursion is: Recursion splits a problem:
Recursion Recursion Recursion is: A problem solving approach, that can... Generate simple solutions to... Certain kinds of problems that... Would be difficult to solve in other ways Recursion splits a
More informationIncrement and the While. Class 15
Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment
More informationCOS 126 General Computer Science Spring Written Exam 1
COS 126 General Computer Science Spring 2017 Written Exam 1 This exam has 9 questions (including question 0) worth a total of 70 points. You have 50 minutes. Write all answers inside the designated spaces.
More informationWhat is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.
Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:
More informationCh 6. Functions. Example: function calls function
Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationESc101 : Fundamental of Computing
ESc101 : Fundamental of Computing I Semester 2008-09 Lecture 37 Analyzing the efficiency of algorithms. Algorithms compared Sequential Search and Binary search GCD fast and GCD slow Merge Sort and Selection
More informationCSCI 135 Software Design and Analysis, C++ Homework 1 Solution
CSCI 135 Software Design and Analysis, C++ Homework 1 Solution Saad Mneimneh Hunter College of CUNY PART I The purpose of PART I is to practice: input/output if statements and constructing the appropriate
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCS/CE 2336 Computer Science II
S/E 2336 omputer Science II UT D Session 10 Recursion dapted from D Liang s Introduction to Java Programming, 8 th Ed 2 factorial(0) = 1; omputing Factorial factorial(n) = n*factorial(n-1); n! = n * (n-1)!
More informationx++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?
x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x? x++ vs. ++x public class Plus{ public static void main(string []args){ int x=0; int a=++x; System.out.println(x); System.out.println(a);
More informationMath 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)
Math 30 Introduction to Proofs via Number Theory Robert Jewett (with small modifications by B. Ćurgus) March 30, 009 Contents 1 The Integers 3 1.1 Axioms of Z...................................... 3 1.
More informationThe American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010
The American University in Cairo Computer Science & Engineering Department CSCE 106-08 Dr. KHALIL Exam II Spring 2010 Last Name :... ID:... First Name:... Form - I EXAMINATION INSTRUCTIONS * Do not turn
More informationISC 2011 COMPUTER SCIENCE PAPER 1 THEORY
ISC 2011 COMPUTER SCIENCE PAPER 1 THEORY Question 1. a) State the two absorption laws. Verify any one of them using truth table. b) Reduce the following expression : F(A,B,C)= (0,1,2,3,4,5,6,7) Also find
More informationAssertions & Verification & Example Loop Invariants Example Exam Questions
2014 November 27 1. Assertions & Verification & Example Loop Invariants Example Exam Questions 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer
More informationThere are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.
There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read. 1. Write a function named largestadjacentsum that iterates
More informationSuggestive List of C++ Programs
Suggestive List of C++ Programs 1. Write a C++ program to display Hello World! on the output screen. 2. Write a program to display Multiplication Table of a number inputted by the user. 3. Write a program
More informationCOMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More informationProgramming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,
More informationAlgorithm Design And Analysis Asst. Prof. Ali Kadhum Idrees The Sum-of-Subsets Problem Department of Computer Science College of Science for Women
The Sum-of-Subsets Problem In this problem, there is a set of items the thief can steal, and each item has its own weight and profit. The thief's knapsack will break if the total weight of the items in
More informationAssertions & Verification Example Exam Questions
2009 November 23 Assertions & Verification Example Exam Questions 1. 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify
More informationPart 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)
EECS 183 Fall 2013 Exam 1 Part 1 (80 points) Closed Book Closed Notes Closed Electronic Devices Closed Neighbor Turn off Your Cell Phones We will confiscate all electronic devices that we see including
More informationCSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR
More informationLoop Invariant Examples
Loop Invariant Examples Page 1 Problems Page 3 Help (Code) Page 5 Solutions (to Help Code) Problems: Exercise 0 @return the average of the list average(double[ ] a) // Yes, this should be as easy as it
More information