d ivide and conquer strategy, d ata type, a lgorithm design and practice. week 13 mr.mohammed...
TRANSCRIPT
DIVIDE AND CONQUER STRATEGY, DATA TYPE, ALGORITHM DESIGN AND PRACTICE.
Week 13
Mr.M
oh
am
me
d R
ah
ma
th
DIVIDE AND CONQUER STRATEGY
A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
Mr.M
oh
am
me
d R
ah
ma
th
ADVANTAGES OF DIVIDE AND CONQUER STRATEGY
Solving difficult problems Algorithm efficiency Parallelism Memory access Roundoff control
Mr.M
oh
am
me
d R
ah
ma
th
DATA TYPE
Data type is define as the type of data. Example: integers, Boolean, characters, floating-
point numbers, alphanumeric strings…
Mr.M
oh
am
me
d R
ah
ma
th
ALGORITHM
Definition : an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.
Mr.M
oh
am
me
d R
ah
ma
th
STEPS IN DEVELOPMENT OF ALGORITHMS
1. Problem definition2. Development of a modal3. Specification of Algorithm4. Designing an Algorithm5. Checking the correctness of Algorithm6. Analysis of Algorithm7. Implementation of Algorithm8. Program testing9. Documentation Preparation
Mr.M
oh
am
me
d R
ah
ma
th
EXAMPLEQ) Write a algorithm to find out number is odd or
even?• step 1 : start• step 2 : input number• step 3 : rem=number mod 2• step 4 : if rem=0 then• print "number even"• else• print "number odd"• endif• step 5 : stop
Mr.M
oh
am
me
d R
ah
ma
th
Q) Write an algorithm to add two numbers entered by user.
Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result
to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
Mr.M
oh
am
me
d R
ah
ma
th
Q) Write an algorithm to check whether a number entered by user is prime or not.
• Step 1: Start • Step 2: Declare variables n,i,flag. • Step 3: Initialize variables flag←1 i←2 • Step 4: Read n from user. • Step 5: Repeat the steps until i<(n/2) 5.1 If remainder of
n÷i equals 0 flag←0 Go to step 6 5.2 i←i+1 • Step 6: If flag=0 Display n is not prime else Display n is
prime • Step 7: Stop
Mr.M
oh
am
me
d R
ah
ma
th
Q) Write an algorithm to find the largest among three different numbers entered by user.
Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b If a>c Display a is the largest
number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number.
Step 5: Stop
Mr.M
oh
am
me
d R
ah
ma
th