Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.

Similar presentations


Presentation on theme: "Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize."— Presentation transcript:

1 Advanced Program Design

2 Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize specification to solve given problem and related problems of same kind Program Development and Problem Solving

3 Review  Step 2: Data organization and algorithm design  Algorithm – a procedure to process input and produce the required output  Data organization – representation of input, output, and intermediate values –assignment of variable names to values Basic description of an algorithm 1.get input 2.compute output for given input values 3.return output

4 Assignment statements  Assigns the value of an expression to a variable:  All variables that appear on the right side of an assignment statement must have previously defined values  The value resulting from the evaluation of the expression is assigned to the variable on the left side of the assignment statement =

5 Examples  Variables with previously assigned values can appear on both sides of the assignment statement pi=3.14159; x=15; y=30; z=x+y; a=80; b=95; average=(a+b)/2; sum=0; sum=sum+1; the equals sign should be interpreted as “is assigned the value of ” or “is replaced by” 

6 Review - Control Structures  Sequential execution –instructions follow one another in a logical progression  Selective execution –provides a choice depending upon whether a logical expression is true or false  Repetitive execution –the same sequence of instructions is to be repeated a number of times

7 Selective execution: if, if-else  if –executes instructions only when the logical expression is true –used to select “special cases”  if-else –executes one set of instructions when the logical expression is true and different instructions when the expression is false –used to select between two cases –example from last class: minimum of two numbers

8 Selective execution: if else-if else  if else-if else –distinguish between three or more cases –example: convert numerical grade to A-F  If a logical expression is true, the remainder of the statements are bypassed –good design - check likeliest cases first if(grade  90) letter_grade = A; else if(grade  80) letter_grade = B; else if(grade  70) letter_grade = C; else if(grade  60) letter_grade = D; else letter_grade = F;

9 Repetitive execution: for-loops  Repetition controlled by a counter –instructions executed once for each value of a variable in a specified range  Example:  If a = 3, b = 7, and x = 10 initially, what is the value of x at the end of the loop? for k = a to b x=x+k; k is the counter variable a,b,x must have assigned values  

10 Repetitive execution: while-loops  Repetition controlled by a logical expression –instructions executed while the logical expression is true –some variable in the logical expression must change with each repetition otherwise, we loop forever! for k = a to b x=x+k; j=a; while(j<b) { x=x+k; j=j+1; } a for loop can also be written as a while loop

11 For-loop or while-loop?  When to use a for-loop: –always for counting! –you know how many times to execute the loop –counter variable is not changed in the loop –counter variable is not used outside the loop  When to use a while-loop: –number of repetitions needed is unknown –drawback: infinite loops be extremely careful when you design while loops!

12 Detecting infinite loops  Problem: compute sum of positive integers  n  Assume n is an input value. Are the following while-loops correct or incorrect? Why? sum=0; while(n>0) sum=sum+n; sum=0; while(n>0) { sum=sum+n; n=n+1; } k=1; sum=0; while(sum<n) { sum=sum+k; k=k+1; }

13 Exercises  Design an algorithm compute sum of all positive integers  n –for example, if n = 5 then your algorithm should return 15 (because 1+2+3+4+5=15)  Design an algorithm that computes x n for an integer x and an integer n  0 –if n = 0, then x 0 = 1 otherwise, x n = x x x x x x n times


Download ppt "Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize."

Similar presentations


Ads by Google