Download presentation
Presentation is loading. Please wait.
Published byKelly Neal Modified over 9 years ago
1
1 MT258 Computer Programming and Problem Solving Unit 7
2
2 UNIT Seven Divide and conquer
3
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.