Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 Advanced Selection CE00371-1: Introduction to Software Development Week 3 Lecture 2.

Similar presentations


Presentation on theme: "0 Advanced Selection CE00371-1: Introduction to Software Development Week 3 Lecture 2."— Presentation transcript:

1 0 Advanced Selection CE00371-1: Introduction to Software Development Week 3 Lecture 2

2 1 In a moment we look at a selection example where we need to be more strategic with the way we design our program. Pseudo-Code A technique called pseudo-code - a highly formal form of English that corresponds to programming language is used for program development. The three variables are now sorted! Orange Box Convention On this module all pseudo-code has an orange box around it. eg give a prompt; read integers a, b, and c ;

3 2 Pseudo-Code is Generic The three variables are now sorted! give a prompt; read integers a, b, and c ; System.out.println("a,b,c ?"); a = keybd.nextInt( ); b = keybd.nextInt( ); c = keybd.nextInt( ); printf("a,b,c ?"); scanf("%d%d%d", &a, &b, &c); Java C Pseudo-Code

4 3 Properties of Pseudo-Code The three variables are now sorted! outlines a basic strategy. cannot be compiled. is generic. can be translated into comments ! is concise

5 4 Sort Example The three variables are now sorted! The requirement is to sort three Integers a,b and c into increasing order. We need to arrange that :- a <= b <= c

6 5 The requirement is to arrange that three integers a,b and c hold values in ascending order. Namely, a ends up with the smallest value, b the middle one, and c the largest one. Sorting Three Integers To solve this problem, a strategy has to be thought out and then translated into our target programming language. After working with pen and paper on a few examples, the following strategy (algorithm) might occur to you. The three variables are now sorted! before :- a 10 b 14 a 5 b 10 c 5 c 14 after :-

7 6 Sorting a, b, and c Shuffle the largest value into c as follows:-. (i) Compare a and b and swap if a is larger. (ii) Compare b and c and swap if b is larger. Shuffle the second largest value into b as follows:- (iii) Compare a and b and swap if a is larger. The above strategy is called an algorithm - it works for all possible values of a,b, and c. On the next page we try it out.

8 7 Dry Running after (i) - no change a 10 b 14 c 5 a 10 b 5 c 14 after (ii) - exchange after (iii) - exchange a 5 b 10 c 14 We’ll check out the strategy for the original data set. NB The state of variables after each step is shown.

9 8 Read in three ints a, b and c. (i) if (a >b) { swap a and b ; } (ii) if (b >c) { swap b and c ; } (iii) if (a >b) { swap a and b ; } Print out a,b and c. Rough Draft of Sort Remember :- To swap a and b, we do t = a ; a=b; b = t ; Similarly swap b and c, is t = b ; b=c; c = t ; pseudo-code

10 9 Step-wise Refinement Read in three ints a,b, c. Prompt for input ; Read a ; Read b ; Read c ; // Prompt for input ; System.out.println("Please enter a,b and c"); a = kybd.nextInt( ); // Read a b = kybd.nextInt( ); // Read b c = kybd.nextInt( ); // Read c pseudo-code java pseudo-code

11 10 Step-wise Refinement if ( a > b ). Swap a and b ; if ( a > b ) { t = a ; a = b ; b = t } And similarly for the other two if then s. pseudo-code java

12 11 Step-wise Refinement Print out a,b and c. System.out.println( " a = " + a + " b = " + b + " c = " + c ) ; pseudo-code java

13 12 import java.util.* ; public class SortThree { public static void main (String[ ] args ) { int a, b, c, t ; Scanner kybd = new Scanner(System.in) ; // Read in a,b and c. System.out.println("Please enter a, b, and c") ; a = kybd. nextInt( ) ; b = kybd. nextInt( ) ; c = kybd. nextInt( ) ; Complete Java Sort for Three integers … continued overpage

14 13 if ( a>b ) // swap (i) { t = a ; a = b ; b= t ; } if ( b>c ) // swap (ii) { t = b; b = c ; c = t ; } if ( a>b ) // swap (iii) { t = a ; a = b ; b= t ; } System.out.println ( "a = " + a + " b= " + b + " c = " + c ) ; }

15 14 Input Validation One of the more important uses of selection is in the area of validation. A user might not interact properly with a program and this should always be checked for. Is data in range ? Is data of the correct type ? The first check above is fairly easy, but we should also bear in mind the second kind of error. For our simple programs we don't consider the latter.

16 15 Range Validation Fragment System.out.println("Please enter Grade ?"); grade = keybd.nextInt( ); if ( grade >=0 && grade <= 100) { System.out.println(" A valid grade "); } else { System.out.println("Error : this is " + "an invalid grade"); } Below we show the validation of a grade which should be in the range 0-100. The user gets an error message if the input is invalid.

17 16 Looped Validation When we meet iteration next week, a user can be re-prompted if their input is invalid. So our programs can be made more robust. NB In industry a huge percentage of the code is "defensive" and complex sequences of if statements protect the "positive" code, in other words, most of the code looks after bad inputs.


Download ppt "0 Advanced Selection CE00371-1: Introduction to Software Development Week 3 Lecture 2."

Similar presentations


Ads by Google