Solving Complex Problems. Review A subroutine is a set of instructions to perform a particular computation –used to solve subproblems of more complex.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Introduction to Analysis of Algorithms
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
ICS103 Programming in C Lecture 9: Functions I
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Programming Fundamentals (750113) Ch1. Problem Solving
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
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 In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Analysis of Algorithms
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Algorithm Design.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 MT258 Computer Programming and Problem Solving Unit 4.
solve x + (-16) = -12 solve x + (-16) = X = 4.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Subroutines. Harder Problems  Examples so far: sum, sum between, minimum –straightforward computation –small number of intermediate variables –single.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
C# Programming Methods.
Functions What is a function –“sub” program, with an isolated set of statements –Accepts parameters, returns a result (perhaps) –Think of it as its own.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Sections 4.1 & 4.2 Recursive Definitions,
Complexity Analysis (Part I)
Function – I What is a function Why we need functions?
User-Written Functions
Chapter 6: User-Defined Functions I
Analysis of Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Java 4/4/2017 Recursion.
User-Defined Functions
CS1120: Recursion.
Programming Fundamentals (750113) Ch1. Problem Solving
Divide and Conquer Algorithms Part I
CS302 - Data Structures using C++
In C Programming Language
Main() { int fact; fact = Factorial(4); } main fact.
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Solving Complex Problems

Review A subroutine is a set of instructions to perform a particular computation –used to solve subproblems of more complex problems –used for computations performed more than once in a single program or that are required in many different programs you only need to write it once Subroutines have names, zero or more parameters, return types, and a body of statements that perform the actual computations

Syntax The return-type specifies the data type of the output The parameter-list is a comma-separated list of the input variables and their types –each list item has the form input-type variable-name A program refers to the subroutine by subroutine-name The body consists of the statements between { } return-type subroutine-name( parameter-list ) { … statements … }

Add two integers int add(int x, int y) { return x+y; }

Minimum of two integers int minimum(int x, int y) { if(x < y) return x; else return y; }

Sum of positive integers int sum_integers(int n) { int k; int sum=0; for(k=1; k<=n; k++) { sum=sum+k; } return sum; }

Factorial int factorial(int n) { int k; int fn=1; for(k=2; k<=n; k++) { fn=fn*k; } return fn; }

What about harder problems? Examples so far: add, minimum, sum, factorial –straightforward computation –small number of intermediate variables –single control structure How do we design algorithms for more complicated problems? –complex computation –many intermediate variables –multiple control structures Often difficult to visualize all the details of a complete solution to a problem

Top-down design –original problem is divided into simpler independent subproblems –successive refinement: these subproblems may require further division into smaller subproblems –continue until all subproblems can be easily solved or you already have a method to solve them

Solving complex problems Divide complex problem into subproblems For each subproblem –analyze the problem do we already have a subroutine to solve it? can we easily design an algorithm to solve it? can it be divided further? –solve the problem Combine solutions of all subproblems to solve the original problem

Minimum of three integers Problem: Find the minimum of three integers

Minimum of three integers Analyze the problem –Inputs x first integer y second integer z third integer –Output min_xyz minimum of x, y and z –How do we find the minimum? we already have a subroutine to find the minimum of two integers …

Top-down design Compute minimum of x, y, and z Compute minimum of x and y Compute minimum of solution to SUBPROBLEM1 and z SUBPROBLEM1 SUBPROBLEM2

Calling subroutines A subroutine call statement has the following syntax: subroutine-name( parameters ); Calling a subroutine executes the statements in the body of the called subroutine using the specified parameters parameters is a comma-separated list of input values –the input values must be of the same type as those in the subroutine’s parameter list The subroutine call statement may be part of an assignment statement or an return statement

Minimum of three integers We can call our minimum subroutine twice to compute the minimum of three integers int min_xy, min_xyz; min_xy = minimum(x,y); min_xyz = minimum(min_xy,z);

Minimum of three integers - subroutine int min3(int x, int y, int z) { int min_xy, min_xyz; min_xy = minimum(x,y); min_xyz = minimum(min_xy,z); return min_xyz; }

Combination Problem: Compute C(n,k)

Definition: combination A combination C(n,k) is the number of ways to select a group of k objects from a population of n distinct objects where n! is the factorial function n! = n*(n–1)*(n-2) … 3*2*1 n! k!(n-k)! C(n,k)=

Top-down design Compute k! Compute n! Compute (n-k)! n! k!(n-k)! Compute C(n,k)= SUBPROBLEM3 SUBPROBLEM2SUBPROBLEM1

Combination - subroutine We can call our factorial subroutine three times to compute C(n,k) int combination(int n, int k) { return factorial(n)/ (factorial(k)*factorial(n-k)); }

Exercises 1.We have a subroutine sum_integers that computes the sum of the first n positive integers Write a subroutine named sum_between to compute the inclusive sum between two integers by making two calls to sum_integers 2.Write a subroutine named min4 to compute the minimum of four numbers