1 MT258 Computer Programming and Problem Solving Unit 7.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Introduction to C Programming
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
MATH 224 – Discrete Mathematics
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
Advanced Topics in Algorithms and Data Structures Page 1 Parallel merging through partitioning The partitioning strategy consists of: Breaking up the given.
VBA Modules, Functions, Variables, and Constants
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
C++ fundamentals.
Introduction to Methods
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 MT258 Computer Programming and Problem Solving Unit 9.
Lecture No.01 Data Structures Dr. Sohail Aslam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CPS120: Introduction to Computer Science Decision Making in Programs.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Learners Support Publications Constructors and Destructors.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Constructors and Destructors
Chapter 10 : Implementing Subprograms
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Recursion DRILL: Please take out your notes on Recursion
5.13 Recursion Recursive functions Functions that call themselves
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 10: Implementing Subprograms Sangho Ha
understanding memory usage by a c++ program
Chapter 4 Functions Objectives
Functions, Part 1 of 3 Topics Using Predefined Functions
Memory Allocation CS 217.
Chapter 7: User-Defined Functions II
Functions, Part 1 of 3 Topics Using Predefined Functions
Dynamic Memory.
COMPILERS Semantic Analysis
Basic Concepts of Algorithm
Functions, Part 1 of 3 Topics Using Predefined Functions
Presentation transcript:

1 MT258 Computer Programming and Problem Solving Unit 7

2 UNIT Seven Divide and conquer

3 Problem-solving process w Requirement gathering and analysis The requirements for the desired computer program or software system are gathered. Some requirements are concerned with the time needed to finish a job, the amount of data to be handled, and so forth. w Specification of the program A formal description of what the computer program will do. w Design of a solution Design of the computer program. w Implementation of the design Write the computer program according to the design w Testing of the implementation Testing the computer program to see if its performance matches the specification.

4 Divide and conquer w Divide up a problem into sub-problems; w Solve the sub-problems, either one by one or at the same time if the sub-problems are independent; w Integrate the solutions of the sub-problems into a final solution to the original problem. w Example Binary search makes use of the principle of divide an conquer to achieve high searching efficiency.

5 Characteristics of divide and conquer w The stopping condition. w The number of sub-problems created when a problem is divided. w The size of sub-problems divided from the original problem. w The method of integrating the solution of sub-problems into the final solution for the original problem. w Example Binary search, please refer Table 7.4 on page 16 of unit 7.

6 Modular programming w A program development approach; w Allows a program to be built from modules; w Allows programmers to develop larger programs more effectively. w Please refer to figure 7.10 on page 17 of unit 7 and table 7.5 on page 18 of unit 7.

7 Advanced topics in variables and memory w Identifiers are names found in a program. Many programming entities, including variables and functions, require a name. No two variables can share the same name in the same block. w Blocks are sections of program code that are grouped together by a pair of curly braces. A function body is considered a block, and a compound statement is also a block.

8 Advanced topics in variables and memory w The use of a variable must within the scope of that variable. w Variables declared inside a block override the variables declared outside the block with the same identifier. w Please refer to page 43 of unit 7.

9 Variables at execution time w Global variables or their associated memory are created when the program begins executions, and they are destroyed when the program ends execution. w Local variables are created when the program execution enters a block, and they are destroyed when the execution leaves the block.

10 Memory usage and function call w The amount of memory used fluctuates as the program is being executed. w When an execution thread enters a function or a block with local variables, these variables are created and memory is therefore acquired from the operating system. w When the execution thread leaves, the local variables are destroyed and the memory is returned back to the operating system. w Please refer to page 46 of unit 7.

11 Dynamic memory allocation w malloc w Syntax : void * malloc(size_t size); w Function : The malloc function allocates a block of memory with the size argument.

12 Dynamic memory allocation w Memory space is used by the program as needed, and can be returned to the system while the program is running, thereby not tying it up unnecessarily. w It may not be necessary to know the list size in advance. w Insertions and deletions may be made without the effort required for contiguous implementations, since there is no copying of large records, and only a few pointers need to be changed.

13 Dynamic memory allocation w Example : w Num = (int*) malloc(sizeof (int)); w if 500 integers needed, Num points to 500 contiguous integers : w Num = (int*) malloc(sizeof (int)*500);

14 Dynamic memory allocation w free w Syntax : void free(void* block); w Function : The free function frees an allocated memory block. w Example : w free(num);

15 Nature of recursion w Recursion solves problems in the following manner: If the problem is simple and a solution is available, then use the solution. If the problem is not simple, divide the problem into two or more sub-problem. The sub- problems should be similar to the original problem. The sub-problems are then solved by recursion. w Please refer to page 59, 62, 64 and 65 of unit 7.

16 Merits of recursion w Pros Recursive solutions look tidy and easy to implement as long as the base cases and general cases are identified. w Cons Recursive solutions are implemented using recursive functions that are slower to execute and consume a lot of memory.