Advanced Program Design Review Program Development and Problem

Advanced Program Design

Review Program Development and Problem Solving ® 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

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

Assignment statements ® Assigns the value of an expression to a variable: <variable> = <expression> ® 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

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

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

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

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;

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

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; a for loop can also be written as a while loop j=a; while(j<b) { x=x+k; j=j+1; }

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!

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; }

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 xn for an integer x and an integer n 0 – if n = 0, then x 0 = 1 otherwise, xn = x • x • x • • • x n times
- Slides: 13